【问题标题】:Parse Json Object in android?在android中解析Json对象?
【发布时间】:2014-03-03 12:48:18
【问题描述】:

我正在开发一个应用程序,我必须从服务器接收数据,我已成功读取数据。在这里我有问题我从 AsyncTask 中编写的服务器代码接收数据,并将数据从 AsyncTask 发送到我的活动,这里我只发送三个数据中的一个,我的 json 对象有 3 个对象。我可以在 AsyncTask 中获取 3 个对象但没有得到在活动中

我的异步任务

 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);
}

    }

我的活动

  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);

  }

【问题讨论】:

  • 尝试 startActivity(intent); 而不是 mContext.startActivity(intent);
  • 您必须发送一个对象数组,用这些对象创建一个类并发送该数组,要发送您的数组,您必须实现parcelable
  • 解析选项,试试Gson:stackoverflow.com/questions/21480634/…

标签: java android json


【解决方案1】:

因为它是一个 jsonArray,现在你只发送最后一个对象而不是整个数组

【讨论】:

  • 是的,最好是制作 get/set 函数并将数组设置为第一个活动并在第二个活动中获取它
  • 这样您就可以解决未直接指出的“通过意图发送数据”问题。
【解决方案2】:

改变这个

 JSONObject mapDetails =        
                      jsonArryDetails.getJSONObject(0);

 JSONObject mapDetails =        
                      jsonArryDetails.getJSONObject(i);

你必须添加类似的东西

   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);

        }

【讨论】:

  • 我必须将所有对象发送到活动中是否可能?
  • @Durga 完成此操作后,您需要查看 this link 以将您的列表发送到活动
  • 谢谢你如何将这个数组列表发送到我的活动中,如何阅读
  • 不,有了这个所有经纬度值,我想在地图上显示位置
【解决方案3】:

在for循环中更改索引:

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

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

【讨论】:

    【解决方案4】:

    您需要创建一个类来存储您正在使用的四个字段(纬度、经度、地址、时间),使该对象具有寓言性,您可以使用以下示例:http://aryo.lecture.ub.ac.id/android-passing-arraylist-of-object-within-an-intent/; 之后,您可以使用以下方法将数组附加到意图:

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

    这将是处理此问题的正确方法,即使它比您最初希望的要复杂一些。

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多