gpt4 book ai didi

java - 在android中解析Json对象?

转载 作者:行者123 更新时间:2023-12-01 17:15:57 25 4
gpt4 key购买 nike

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

我的异步任务

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

}

我的 Activity

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

}

最佳答案

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

关于java - 在android中解析Json对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22147242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com