简体   繁体   中英

passing object through intent from background service to an activity

I want to pass a custom made vector object containing data from background service to an activity in order to updating UI of that activity. I am not able to pass my data via intent. Please assist me what i need to do..

Following is the code snippet that i am using. In below code I want to pass article list to an activity.

Vector<RowData> getArticleLog() throws JSONException{

    Vector<RowData> articleList = new Vector<RowData>();

    RowData rd;
    InputStream is = null;
    String result = null;
    JSONArray jarray = new JSONArray();

    //Add data to be send.
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("article_title", "Flying Horse"));

    try {

        HttpClient httpclient = new DefaultHttpClient();

        // for local server xampp
        HttpPost httppost = new HttpPost("http://10.0.2.2/groupbook/return_article_log.php");

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);

        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.i("postData", response.getStatusLine().toString());

    } catch (Exception e) {
        Log.e(TAG, "json error : " + e.toString());
    }
    //convert response to string
    try {
         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
         StringBuilder sb = new StringBuilder();
         String line = null;
         while ((line = reader.readLine()) != null) {
                 sb.append(line + "\n");
         }
         is.close();

         result=sb.toString();
    } catch (Exception e) {
         Log.e(TAG, "Error converting result "+e.toString());
    }

    try {
        jarray = new JSONArray(result);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //return jArray in the form of Array list;
    JSONObject rowElement = null;
    int viewers = 0;
    int votes = 0;

    for (int i = 0; i < jarray.length(); i++) {
        rowElement = jarray.getJSONObject(i);

        viewers = rowElement.getInt("viewers");
        votes = rowElement.getInt("votes");

        rd = new RowData(i, 
                rowElement.getString("article_title"), 
                Integer.toString(i), 
                rowElement.getString("last_post_by"),
                "2 hours", 
                rowElement.getString("catagory_tag"), 
                Integer.toString(viewers), 
                rowElement.getString("privacy_tag"), 
                Integer.toString(votes), 
                rowElement.getString("catagory_title"));
        articleList.add(rd);
    }

    return articleList;
} 

RowData class is as follows :

public class RowData {

protected int mId;
public String articleTitle;
public String articleCounter;
public String lastPostBy;
public String updatedTime;
public String catagoryTag;
public String viewingNow;
public String privacyTag;
public String votes;
public String catagoryTitle;

public RowData(int mId, String articleTitle, String articleCounter,
        String lastPostBy, String updatedTime, String catagoryTag,
        String viewingNow, String privacyTag, String votes,
        String catagoryTitle) {
    this.mId = mId;
    this.articleTitle = articleTitle;
    this.articleCounter = articleCounter;
    this.lastPostBy = lastPostBy;
    this.updatedTime = updatedTime;
    this.catagoryTag = catagoryTag;
    this.viewingNow = viewingNow;
    this.privacyTag = privacyTag;
    this.votes = votes;
    this.catagoryTitle = catagoryTitle;
}
}

Is following way is right way??

private void DisplayingInfo() throws JSONException{
    Log.d(TAG, "entered DisplayLoggingInfo");
    intent.putExtra("articleLog", getArticleLog());
    sendBroadcast(intent);
}

One thing you can do is to make your custom class implement Serializable (or Parcelable ), send the serializable object with putExtra() method to the activity and use getSerializableExtra() on your activity to get it.


Edit 1: a quick example:

In your custom class:

import java.io.Serializable;

@SuppressWarnings("serial")
public class YourCustomVectorClass implements Serializable {
    // ...
}

In your service where you want to start the activity:

Intent intent = new Intent(yourContext, yourActivity.class);
intent.putExtra("theNameOfTheObject", yourObject);
startActivity(intent);

In your activity:

YourCustomVectorClass yourVector = (YourCustomVectorClass) getIntent().getSerializableExtra("theNameOfTheObject");

Edit 2: After reading the question again, I realized that you're passing a Vector of RowData objects to your Activity.

Since Java Vector class implements Serializable , I think you shouldn't do anything but passing the vector to the activity using putExtra() and get it with getSerializableExtra() on the Activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM