![](/img/trans.png)
[英]What is best practice to call any other servlet in other JVM from some other JVM
[英]what is the best practice for getting JSON from other activity
我想知道获取JSON并显示它的最佳实践是什么,从一个到活动(没有GUI)再到另一个活动(有GUI),我发现了几种方法,从只在PutExtra中传递jsonobject到制作一个Parcable类,然后将其作为PutExtraParacble传递。 但是我发现它是不完整的,因为获取json的时间可能从1milisec到5整秒不等(将要有samll和巨大的Jsons),所以当我使用parcable类方法然后从MainActivity获取其他内容时,我可以从中获取null指针异常parcable类...这是重要的代码部分:
APIcontroller.getCardAPI("http://www.somesite.com/feeds/GetPlace", params);
Intent getJson = getIntent();
CardContent cardContent = (CardContent) getJson.getParcelableExtra("cardContent");
jsonArryCard = cardContent.getJsonArray();
然后在APIcontroller类中:
public void getCardAPI(String WSaddress,RequestParams params) {
AsyncHttpClient client = new AsyncHttpClient();
client.post(WSaddress,params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String respons = null;
try {
respons = responseBody == null ? null : new String(responseBody, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
JSONObject responseObject = new JSONObject(respons);
JSONArray resultsArray = responseObject.getJSONArray("places");
JSONObject taskObject = null;
for (int i = 0; i < resultsArray.length(); i++) {
// the JSON object in position i
taskObject = resultsArray.getJSONObject(i);
String temp = (String) taskObject.get("UserName");
Log.e("String test", temp);
}
CardContent cardContent = new CardContent(resultsArray);
Intent sendjson = new Intent(APIController.this, MainRegisterdActivity.class);
sendjson.putExtra("cardContent", (Parcelable) cardContent);
startActivity(sendjson);
MainRegisterdActivity.class);
Log.e("onSuccess", String.valueOf(taskObject));
} catch (JSONException e) {
e.printStackTrace();
}
}
我认为这是因为asyntask仍在运行,所以当它进入下一个代码行时,它会有一个值。.我应该只使用广播接收器还是有其他方法? 谢谢你的帮助
您在想对! 我也有相同的经验,我认为最好的方法是使用广泛的广播接收器。 另一个不整洁的解决方案是将getCardAPI
函数从类中移出并通过GUI进行活动,然后将其余代码放入getCardAPI
函数中。因此,您可以确定已设置了值,并且将执行其余代码,但是不推荐!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.