如何在意图中发送Jsoup文档?

时间:2018-10-26 17:28:17

标签: android gson jsoup localbroadcastmanager

我无法按意图发送对象。我读到gsoup是完成此任务的一种方法,但我无法使其正常工作。

这是一个例子。此代码尝试获取此stackoverflow问题的标题。它使用jsoupgsonIntentServiceLocalBroadcastManager。它不起作用,因为它会导致错误:java.lang.StackOverflowError

MainActivity:

package com.example.elk.gsonbug;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import com.google.gson.Gson;
import org.jsoup.nodes.Document;

public class MainActivity extends AppCompatActivity {

    private BroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(JsonIntentService.ACTION_GET_QUESTION_TITLE)) {
                    Document documentQuestionTitle = new Gson().fromJson(intent.getStringExtra(JsonIntentService.EXTRA_QUESTION_TITLE_DOCUMENT), Document.class);
                    String question = documentQuestionTitle.selectFirst("div#question-header > h1").text();
                    Toast.makeText(context, question, Toast.LENGTH_LONG).show();
                }
            }
        };
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(JsonIntentService.ACTION_GET_QUESTION_TITLE));


        JsonIntentService.startActionGetQuestionTitle(this, "https://stackoverflow.com/questions/53013644/how-to-send-jsoup-document-in-an-intent");
    }
}

JsonIntentService:

package com.example.elk.gsonbug;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.support.v4.content.LocalBroadcastManager;

import com.google.gson.Gson;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class JsonIntentService extends IntentService {
    static final String ACTION_GET_QUESTION_TITLE = "com.example.elk.gsonbug.action.GET_QUESTION_TITLE";
    private static final String EXTRA_URL = "com.example.elk.gsonbug.extra.URL";
    static final String EXTRA_QUESTION_TITLE_DOCUMENT = "com.example.elk.gsonbug.extra.QUESTION_TITLE_DOCUMENT";

    public JsonIntentService() {
        super("JsonIntentService");
    }

    public static void startActionGetQuestionTitle(Context context, String url) {
        Intent intent = new Intent(context, JsonIntentService.class);
        intent.setAction(ACTION_GET_QUESTION_TITLE);
        intent.putExtra(EXTRA_URL, url);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (action.equals(ACTION_GET_QUESTION_TITLE)) {
                final String url = intent.getStringExtra(EXTRA_URL);
                handleActionGetQuestionTitle(url);
            }
        }
    }

    private void handleActionGetQuestionTitle(String url) {
        Document documentQuestionTitle = null;
        try {
            Connection.Response responseDocumentQuestionTitle = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    .execute();
            documentQuestionTitle = responseDocumentQuestionTitle.parse();

            Intent intent = new Intent();
            intent.setAction(ACTION_GET_QUESTION_TITLE);
            intent.putExtra(EXTRA_QUESTION_TITLE_DOCUMENT, new Gson().toJson(documentQuestionTitle, Document.class));
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

编辑#1

这是一个更简单的示例(没有显示问题...) MainActivity调用SecondActivity,后者创建了一个Person对象,并使用gson将其发送回。它可以正常工作:D/MainActivity: Person{name='Bob', age=33} 这是否意味着Document类中的某些内容与gson不兼容?

MainActivity:

package com.example.elk.gsonbug2;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private static final int REQUEST_PERSON = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, SecondActivity.class);
        startActivityForResult(intent, REQUEST_PERSON);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_PERSON) {
            if (resultCode == Activity.RESULT_OK) {
                Person person = new Gson().fromJson(data.getStringExtra(SecondActivity.EXTRA_PERSON), Person.class);
                Log.d(TAG, person.toString());
            }
        }
    }
}

SecondActivity:

package com.example.elk.gsonbug2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.google.gson.Gson;

public class SecondActivity extends AppCompatActivity {

    static final String EXTRA_PERSON = "com.example.elk.gsonbug2.extra.PERSON";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = new Intent();
        Person person = new Person("Bob", 33);
        intent.putExtra(EXTRA_PERSON, new Gson().toJson(person));
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

}

2 个答案:

答案 0 :(得分:0)

在MainActivity中发出请求,这样您就不必担心来回发送对象。

答案 1 :(得分:0)

按意图发送整个对象的目的是什么?如果您需要将任何大的非原始对象从一个活动发送到另一个活动,则将该对象存储到SQLite数据库,ORM或内部/外部存储中,并仅通过意图发送其ID。