[英]Android volley saving json data to string for sharing (onPostExecute method for volley?)
我正在使用凌空库从JSON格式的api中获取数据,它的工作原理很吸引人。 我请求JSON文件,得到响应,我解析响应并在活动中填充视图。
现在,我有一个按钮可以使用Intetnt.ACTION_SEND和putExtra(Intent.EXTRA_TEXT)共享数据,该参数将字符串作为第二个参数。
问题是,我制作了一个字符串变量,并在响应方法的凌空内部将数据追加到该字符串变量中。 但是当齐射创建了一个新线程来从api获取数据时,该字符串未更新,并且Intetnt.EXTRA_TEXT发送了一个空字符串。
我想问一下,对于凌空的onPostExecute方法是否有任何类似之处? 在线程完成处理后,我可以在其中将数据设置为某些变量。 或其他替代方法来执行相同操作。
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, bookSearchString, null,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {
JSONArray bookArray = response
.getJSONArray("items");
JSONObject bookObject = bookArray.getJSONObject(0);
try {
bookSelfLink = bookObject.getString("selfLink");
JsonObjectRequest newJsObjRequest = new JsonObjectRequest(
Request.Method.GET, bookSelfLink, null,
new Response.Listener<JSONObject>() {
public void onResponse(
JSONObject response) {
try {
JSONObject newBookObject = response
.getJSONObject("volumeInfo");
try {
dBookPages.setText("Pages - "
+ newBookObject
.getString("pageCount"));
eMailText = eMailText + newBookObject
.getString("pageCount"));
} catch (JSONException jse) {
dBookPages
.setText("Pages not found");
jse.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(
VolleyError error) {
// TODO Auto-generated method
// stub
}
});
AppController.getInstance().addToRequestQueue(
newJsObjRequest, tag_json_obj);
} catch (JSONException jse) {
jse.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
AppController.getInstance().addToRequestQueue(jsObjRequest,
tag_json_obj);
从Volley v1.0.0文档中获得:
public boolean hasHadResponseDelivered()
如果此请求已传递响应,则返回true。
我希望这个答案可以帮助有相同问题的人。
有2个解决方案可供记录。
1)点击按钮->等待响应->在带有绑定数据的onResponse内触发意图的网络调用。
2)将URL作为字符串传递给包->新活动的调用意图->使网络请求解析数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.