0

I am developing one application in that i have to receive data from server ,I am successfully read data . here i have problem i receive data from server code wrote in AsyncTask,and send data from AsyncTask to My activity,here i send only one data out of three,my json object have 3 objects.i can get 3 objects in AsyncTask but not getting in Activity

my AsyncTask

 public class ReceivingLatLongAsync extends AsyncTask<Void, Void, Void> {

private ProgressDialog pDialog;

Context mContext;

JSONArray jsonArryDetails=null;

public static final String DETAILS = "locations";
public static final String LAT = "lat";
public static final String LNG = "lng";
public static final String ADDRESS = "address";
public static final String CTIME = "ctime";

private String lat1;
private String lng1;
private String address1;
private String time1;


public ReceivingLatLongAsync(Context context){

    this.mContext = context;

}
@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
    pDialog = new ProgressDialog(mContext);
     pDialog.setMessage("Please wait...");
     pDialog.setCancelable(false);
     pDialog.show();

}

@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub

    ServiceHandler serviceHandler= new ServiceHandler();

    String jSonString = `serviceHandler.makeServiceCall
         (TrafficConstants.RECIEVE_LATLON_POL_URL, ServiceHandler.POST);`

    Log.e("Response: ", "> " + jSonString);

    if(jSonString != null){

        try {
            JSONObject jsonObject = new JSONObject(jSonString);

            jsonArryDetails = jsonObject.getJSONArray(DETAILS);

            for(int i = 0;i<jsonArryDetails.length();i++){

                JSONObject mapDetails =        
                          jsonArryDetails.getJSONObject(0);

                lat1 = mapDetails.getString(LAT);
                lng1 = mapDetails.getString(LNG);
                address1 = mapDetails.getString(ADDRESS);
                time1 = mapDetails.getString(CTIME);


                Log.e("ADDRESS1", address1);
                Log.e("TIME2",time1);



            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return null;
}
@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    pDialog.dismiss();

    Intent intent = new Intent(mContext,GetLatLongForTPActivity.class);
    intent.putExtra("LAT", lat1);
    intent.putExtra("LNG", lng1);
    intent.putExtra("ADDRESS", address1);
    intent.putExtra("time",time1);

    mContext.startActivity(intent);
}

    }

my activty

  public class GetLatLongForTPActivity extends FragmentActivity 
                                implements LocationListener{

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

    timeEdit = (EditText)findViewById(R.id.timeId);
    submitBtn = (Button)findViewById(R.id.subId);   

     Intent intent = getIntent();
     String anotherLAT=intent.getStringExtra("LAT");
     String anotherLNG=intent.getStringExtra("LNG");

     Log.e(" NEW LATLONG",anotherLAT);

  }
3
  • try startActivity(intent); not mContext.startActivity(intent); Commented Mar 3, 2014 at 12:51
  • you must send one Array of object, create one class with those object and send that array, for sending your array you must implement parcelable Commented Mar 3, 2014 at 12:54
  • for parsing option, try Gson: stackoverflow.com/questions/21480634/… Commented Mar 3, 2014 at 12:54

4 Answers 4

3

Becouse it is an jsonArray and now you send only the last object not the entier array

Sign up to request clarification or add additional context in comments.

2 Comments

yes the best is to make get/set function and set the array on fist activity and take it with get in second activity
and with that you resolve the "send data via intent" problem that is not directly indicated.
2

Change this

 JSONObject mapDetails =        
                      jsonArryDetails.getJSONObject(0);

to

 JSONObject mapDetails =        
                      jsonArryDetails.getJSONObject(i);

You have to add something like

   ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

   for(int i = 0;i<jsonArryDetails.length();i++){
            HashMap<String, String> map = new HashMap<String, String>();
            JSONObject mapDetails =        
                      jsonArryDetails.getJSONObject(i);

            lat1 = mapDetails.getString(LAT);
            lng1 = mapDetails.getString(LNG);
            address1 = mapDetails.getString(ADDRESS);
            time1 = mapDetails.getString(CTIME);
            map.put(LAT, lat1);
            map.put(LNG, lg1);
            map.put(ADDRESS, address1 );
            map.put(CTIME, time1 );

            mylist.add(map);

        }

4 Comments

i have to send all objects to activity is it possible?
@Durga after do this you need see this link for sending your list to activity
thank you how to send this array list to my activity,how to read in that
no ,with this all lat long values i want to show locations on map
0

In for loop change the index :

        for(int i = 0;i<jsonArryDetails.length();i++){

            JSONObject mapDetails =jsonArryDetails.getJSONObject(i);
            //etc                                                ^ //change here

Comments

0

You need to create a class that stores the four fields you are using (lat,long,address,time), make that object parable, you could use this example: http://aryo.lecture.ub.ac.id/android-passing-arraylist-of-object-within-an-intent/ ; And after that you can attach the array to the intent using:

intent.putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

This would be the correct way to handle this, even if it's a little more complicated then you originally would have hoped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.