Передача данных с помощью намерения из класса с расширением runtimeException [duplicate]

Образец уже показывает это. Посмотрите, например. как генерируются сообщения об ошибках:

// Returns a not found response
auto const not_found = [&req](boost::beast::string_view target) {
    http::response<http::string_body> res{ http::status::not_found, req.version() };
    res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
    res.set(http::field::content_type, "text/html");
    res.keep_alive(req.keep_alive());
    res.body() = "The resource '" + target.to_string() + "' was not found.";
    res.prepare_payload();
    return res;
};

Просто установите body() на что-то другое.

DEMO

Полная демонстрация, в основном, просто зачищая образец из ненужного кода и используя prepare_payload, чтобы автоматически получить длину содержимого / кодирование.

#include <algorithm>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/strand.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/config.hpp>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>

using tcp = boost::asio::ip::tcp;              // from <boost/asio/ip/tcp.hpp>
namespace http = boost::beast::http;           // from <boost/beast/http.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>

// This function produces an HTTP response for the given request.
template <class Body, class Allocator, class Send>
void handle_request(http::request<Body, http::basic_fields<Allocator> > &&req, Send &&send) {
    // Returns a bad request response
    auto const bad_request = [&req](boost::beast::string_view why) {
        http::response<http::string_body> res{ http::status::bad_request, req.version() };
        res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
        res.set(http::field::content_type, "text/html");
        res.keep_alive(req.keep_alive());
        res.body() = why.to_string();
        res.prepare_payload();
        return res;
    };

    // Make sure we can handle the method
    if (req.method() != http::verb::get)
        return send(bad_request("Unsupported HTTP-method"));

    // Request path must be absolute and not contain "..".
    auto target = req.target();
    if (target.empty() || target[0] != '/' || target.find("..") != boost::beast::string_view::npos)
        return send(bad_request("Illegal request-target"));

    http::response<http::string_body> res{ http::status::ok, req.version() };
    res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
    res.set(http::field::content_type, "text/html");
    res.body() = "You're looking at " + target.to_string();
    res.prepare_payload();
    res.keep_alive(req.keep_alive());
    return send(std::move(res));
}

// Report a failure
void fail(boost::system::error_code ec, char const *what) { std::cerr << what << ": " << ec.message() << "\n"; }

// Echoes back all received WebSocket messages
class websocket_session : public std::enable_shared_from_this<websocket_session> {
    websocket::stream<tcp::socket> ws_;
    boost::asio::strand<boost::asio::io_context::executor_type> strand_;
    boost::asio::steady_timer timer_;
    boost::beast::multi_buffer buffer_;

  public:
    // Take ownership of the socket
    explicit websocket_session(tcp::socket socket)
            : ws_(std::move(socket)), strand_(ws_.get_executor()),
              timer_(ws_.get_executor().context(), (std::chrono::steady_clock::time_point::max)()) {}

    // Start the asynchronous operation
    template <class Body, class Allocator> void run(http::request<Body, http::basic_fields<Allocator> > req) {
        // Run the timer. The timer is operated
        // continuously, this simplifies the code.
        on_timer({});

        // Set the timer
        timer_.expires_after(std::chrono::seconds(15));

        // Accept the websocket handshake
        ws_.async_accept(req,
                         boost::asio::bind_executor(strand_, std::bind(&websocket_session::on_accept,
                                                                       shared_from_this(), std::placeholders::_1)));
    }

    // Called when the timer expires.
    void on_timer(boost::system::error_code ec) {
        if (ec && ec != boost::asio::error::operation_aborted)
            return fail(ec, "timer");

        // Verify that the timer really expired since the deadline may have moved.
        if (timer_.expiry() <= std::chrono::steady_clock::now()) {
            // Closing the socket cancels all outstanding operations. They
            // will complete with boost::asio::error::operation_aborted
            ws_.next_layer().shutdown(tcp::socket::shutdown_both, ec);
            ws_.next_layer().close(ec);
            return;
        }

        // Wait on the timer
        timer_.async_wait(boost::asio::bind_executor(
            strand_, std::bind(&websocket_session::on_timer, shared_from_this(), std::placeholders::_1)));
    }

    void on_accept(boost::system::error_code ec) {
        // Happens when the timer closes the socket
        if (ec == boost::asio::error::operation_aborted)
            return;

        if (ec)
            return fail(ec, "accept");

        // Read a message
        do_read();
    }

    void do_read() {
        // Set the timer
        timer_.expires_after(std::chrono::seconds(15));

        // Read a message into our buffer
        ws_.async_read(buffer_,
                       boost::asio::bind_executor(strand_, std::bind(&websocket_session::on_read, shared_from_this(),
                                                                     std::placeholders::_1, std::placeholders::_2)));
    }

    void on_read(boost::system::error_code ec, std::size_t bytes_transferred) {
        boost::ignore_unused(bytes_transferred);

        // Happens when the timer closes the socket
        if (ec == boost::asio::error::operation_aborted)
            return;

        // This indicates that the websocket_session was closed
        if (ec == websocket::error::closed)
            return;

        if (ec)
            fail(ec, "read");

        // Echo the message
        ws_.text(ws_.got_text());
        ws_.async_write(buffer_.data(),
                        boost::asio::bind_executor(strand_, std::bind(&websocket_session::on_write, shared_from_this(),
                                                                      std::placeholders::_1, std::placeholders::_2)));
    }

    void on_write(boost::system::error_code ec, std::size_t bytes_transferred) {
        boost::ignore_unused(bytes_transferred);

        // Happens when the timer closes the socket
        if (ec == boost::asio::error::operation_aborted)
            return;

        if (ec)
            return fail(ec, "write");

        // Clear the buffer
        buffer_.consume(buffer_.size());

        // Do another read
        do_read();
    }
};

// Handles an HTTP server connection
class http_session : public std::enable_shared_from_this<http_session> {
    // This queue is used for HTTP pipelining.
    class queue {
        enum {
            // Maximum number of responses we will queue
            limit = 8
        };

        // The type-erased, saved work item
        struct work {
            virtual ~work() = default;
            virtual void operator()() = 0;
        };

        http_session &self_;
        std::vector<std::unique_ptr<work> > items_;

      public:
        explicit queue(http_session &self) : self_(self) {
            static_assert(limit > 0, "queue limit must be positive");
            items_.reserve(limit);
        }

        // Returns `true` if we have reached the queue limit
        bool is_full() const { return items_.size() >= limit; }

        // Called when a message finishes sending
        // Returns `true` if the caller should initiate a read
        bool on_write() {
            BOOST_ASSERT(!items_.empty());
            auto const was_full = is_full();
            items_.erase(items_.begin());
            if (!items_.empty())
                (*items_.front())();
            return was_full;
        }

        // Called by the HTTP handler to send a response.
        template <bool isRequest, class Body, class Fields>
        void operator()(http::message<isRequest, Body, Fields> &&msg) {
            // This holds a work item
            struct work_impl : work {
                http_session &self_;
                http::message<isRequest, Body, Fields> msg_;

                work_impl(http_session &self, http::message<isRequest, Body, Fields> &&msg)
                        : self_(self), msg_(std::move(msg)) {}

                void operator()() {
                    http::async_write(self_.socket_, msg_,
                                      boost::asio::bind_executor(
                                          self_.strand_, std::bind(&http_session::on_write, self_.shared_from_this(),
                                                                   std::placeholders::_1, msg_.need_eof())));
                }
            };

            // Allocate and store the work
            items_.emplace_back(new work_impl(self_, std::move(msg)));

            // If there was no previous work, start this one
            if (items_.size() == 1)
                (*items_.front())();
        }
    };

    tcp::socket socket_;
    boost::asio::strand<boost::asio::io_context::executor_type> strand_;
    boost::asio::steady_timer timer_;
    boost::beast::flat_buffer buffer_;
    http::request<http::string_body> req_;
    queue queue_;

  public:
    // Take ownership of the socket
    explicit http_session(tcp::socket socket)
            : socket_(std::move(socket)), strand_(socket_.get_executor()),
              timer_(socket_.get_executor().context(), (std::chrono::steady_clock::time_point::max)()), queue_(*this) {}

    // Start the asynchronous operation
    void run() {
        // Run the timer. The timer is operated
        // continuously, this simplifies the code.
        on_timer({});

        do_read();
    }

    void do_read() {
        // Set the timer
        timer_.expires_after(std::chrono::seconds(15));

        // Read a request
        http::async_read(socket_, buffer_, req_,
                         boost::asio::bind_executor(
                             strand_, std::bind(&http_session::on_read, shared_from_this(), std::placeholders::_1)));
    }

    // Called when the timer expires.
    void on_timer(boost::system::error_code ec) {
        if (ec && ec != boost::asio::error::operation_aborted)
            return fail(ec, "timer");

        // Verify that the timer really expired since the deadline may have moved.
        if (timer_.expiry() <= std::chrono::steady_clock::now()) {
            // Closing the socket cancels all outstanding operations. They
            // will complete with boost::asio::error::operation_aborted
            socket_.shutdown(tcp::socket::shutdown_both, ec);
            socket_.close(ec);
            return;
        }

        // Wait on the timer
        timer_.async_wait(boost::asio::bind_executor(
            strand_, std::bind(&http_session::on_timer, shared_from_this(), std::placeholders::_1)));
    }

    void on_read(boost::system::error_code ec) {
        // Happens when the timer closes the socket
        if (ec == boost::asio::error::operation_aborted)
            return;

        // This means they closed the connection
        if (ec == http::error::end_of_stream)
            return do_close();

        if (ec)
            return fail(ec, "read");

        // See if it is a WebSocket Upgrade
        if (websocket::is_upgrade(req_)) {
            // Create a WebSocket websocket_session by transferring the socket
            std::make_shared<websocket_session>(std::move(socket_))->run(std::move(req_));
            return;
        }

        // Send the response
        handle_request(std::move(req_), queue_);

        // If we aren't at the queue limit, try to pipeline another request
        if (!queue_.is_full())
            do_read();
    }

    void on_write(boost::system::error_code ec, bool close) {
        // Happens when the timer closes the socket
        if (ec == boost::asio::error::operation_aborted)
            return;

        if (ec)
            return fail(ec, "write");

        if (close) {
            // This means we should close the connection, usually because
            // the response indicated the "Connection: close" semantic.
            return do_close();
        }

        // Inform the queue that a write completed
        if (queue_.on_write()) {
            // Read another request
            do_read();
        }
    }

    void do_close() {
        // Send a TCP shutdown
        boost::system::error_code ec;
        socket_.shutdown(tcp::socket::shutdown_send, ec);

        // At this point the connection is closed gracefully
    }
};

//------------------------------------------------------------------------------

// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener> {
    tcp::acceptor acceptor_;
    tcp::socket socket_;

  public:
    listener(boost::asio::io_context &ioc, tcp::endpoint endpoint) : acceptor_(ioc), socket_(ioc) {
        boost::system::error_code ec;

        // Open the acceptor
        acceptor_.open(endpoint.protocol(), ec);
        if (ec) {
            fail(ec, "open");
            return;
        }

        // Bind to the server address
        acceptor_.bind(endpoint, ec);
        if (ec) {
            fail(ec, "bind");
            return;
        }

        // Start listening for connections
        acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
        if (ec) {
            fail(ec, "listen");
            return;
        }
    }

    // Start accepting incoming connections
    void run() {
        if (!acceptor_.is_open())
            return;
        do_accept();
    }

    void do_accept() {
        acceptor_.async_accept(socket_, std::bind(&listener::on_accept, shared_from_this(), std::placeholders::_1));
    }

    void on_accept(boost::system::error_code ec) {
        if (ec) {
            fail(ec, "accept");
        } else {
            // Create the http_session and run it
            std::make_shared<http_session>(std::move(socket_))->run();
        }

        // Accept another connection
        do_accept();
    }
};

//------------------------------------------------------------------------------

int main(int argc, char *argv[]) {
    // Check command line arguments.
    if (argc != 4) {
        std::cerr << "Usage: advanced-server <address> <port> <threads>\n"
                  << "Example:\n"
                  << "    advanced-server 0.0.0.0 8080 1\n";
        return EXIT_FAILURE;
    }
    auto const address = boost::asio::ip::make_address(argv[1]);
    auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
    auto const threads = std::max<int>(1, std::atoi(argv[3]));

    // The io_context is required for all I/O
    boost::asio::io_context ioc{ threads };

    // Create and launch a listening port
    std::make_shared<listener>(ioc, tcp::endpoint{ address, port })->run();

    // Run the I/O service on the requested number of threads
    std::vector<std::thread> v;
    v.reserve(threads - 1);
    for (auto i = threads - 1; i > 0; --i)
        v.emplace_back([&ioc] { ioc.run(); });
    ioc.run();

    return EXIT_SUCCESS;
}
107
задан user2896762 17 April 2014 в 21:40
поделиться

7 ответов

Caused by: java.io.NotSerializableException: com.resources.student_list.DSLL$DNode

У вашего класса DSLL есть статический внутренний класс DNode, а DNode не Serializable.

218
ответ дан CommonsWare 27 August 2018 в 08:23
поделиться

Я также фазу этой ошибки, и я немного изменен в modelClass, которые реализованы. Сериализуемый интерфейс, такой как:

. В этом классе модели также реализуется интерфейс Parcelable с методом переопределения writeToParcel ()

Затем появилась ошибка для «create creator» , поэтому CREATOR записывает, а также создает с помощью contclor modelclass с аргументами & amp; без аргументов ..

       @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(id);
            dest.writeString(name);
        }

        protected ArtistTrackClass(Parcel in) {
            id = in.readString();
            name = in.readString();
        }

       public ArtistTrackClass() {

        }

    public static final Creator<ArtistTrackClass> CREATOR = new Creator<ArtistTrackClass>() {
        @Override
        public ArtistTrackClass createFromParcel(Parcel in) {
            return new ArtistTrackClass(in);
        }

        @Override
        public ArtistTrackClass[] newArray(int size) {
            return new ArtistTrackClass[size];
        }
    };

Здесь

ArtistTrackClass -> ModelClass

Конструктор с аргументами Parcel "читает наши атрибуты" и writeToParcel () - «напишите наши атрибуты»

0
ответ дан Dhruv Raval 27 August 2018 в 08:23
поделиться

Для меня это было разрешено, сделав переменную внутри переходного класса.

Код до:

public class UserLocation implements Serializable {
   public Location lastKnownLocation;
   public UserLocation() {}
}

код после

public class UserLocation implements Serializable {
    public transient Location lastKnownLocation;
    public UserLocation() {}
}   
1
ответ дан Gomez NL 27 August 2018 в 08:23
поделиться

Ваш класс OneThread также должен реализовывать Serializable. Все подклассы и внутренние подклассы должны реализовывать Serializable.

это работает для меня ...

19
ответ дан Pankaj Talaviya 27 August 2018 в 08:23
поделиться

Проблема возникает, когда ваш пользовательский класс имеет для свойства какой-либо другой класс, например. "Битовая карта". То, что я сделал, - это изменить поле свойства с «частной растровой фотографии» на «частную временную растровую фотографию». Однако после ввода getIntent () в работе приемника изображение пуст. Из-за этого я передал пользовательский класс в намерение, а также создал массив байтов из изображения и передал его отдельно для намерения:

selectedItem - мой пользовательский объект, а getPlacePhoto - его метод получения образ. Я уже установил его раньше, и теперь я просто получаю его сначала, чем конвертирую его и передаю его разделительно:

 Bitmap image = selectedItem.getPlacePhoto();
 image.compress(Bitmap.CompressFormat.PNG, 100, stream);
 byte[] byteArray = stream.toByteArray();
 Intent intent = new Intent(YourPresentActivity.this, 
 TheReceiverActivity.class);
 intent.putExtra("selectedItem", selectedItem);                 
 intent.putExtra("image", byteArray);
 startActivity(intent);            

`

Затем в работе получателя я получаю свой объект и изображение в виде массива байтов, декодирование изображения и установка его в качестве объекта фотографии.

 Intent intent = getIntent();
 selectedItem = (ListItem) intent.getSerializableExtra("selectedItem");
 byte[] byteArray = getIntent().getByteArrayExtra("image");
 Bitmap image = BitmapFactory.decodeByteArray(byteArray, 0, 
 byteArray.length);
 selectedItem.setPhoto(image);
2
ответ дан Pavel Aslanov 27 August 2018 в 08:23
поделиться

Если вы не можете сделать сериализацию DNode хорошим решением, нужно добавить «переходный» к переменной.

Пример:

public static transient DNode dNode = null;

Это игнорирует переменную, если используя Intent.putExtra (...).

21
ответ дан The Clansman 27 August 2018 в 08:23
поделиться

класс класса должен также реализовать Serializable

public class Grade implements Serializable {
.....your content....
}
0
ответ дан yOshi 27 August 2018 в 08:23
поделиться
Другие вопросы по тегам:

Похожие вопросы: