【发布时间】:2015-01-05 09:28:49
【问题描述】:
我正在使用 Volley,但我在解析 JSON 数据时遇到了一些问题,这很可能是因为 volley 没有实现 AsyncTask 的 onPostExecute() 之类的东西,而且我在错误的列表项上得到了一些重复的数据。
然后我遇到了这个:https://github.com/yakivmospan/volley-request-manager#custom-listener-implementation-
有人用过吗?如何将它添加到我当前的 Volley 代码中?
更多关于我的问题的细节在这里Volley not sending correct data. How to implement an alternative to onPostExecute()?
更新
根据要求,一些代码。这是一个按钮,它调用另一个类的方法,该类使用 Volley 请求一些原始 JSON 数据 (NovaJSON),然后将 JSON 发送到解析器类 (NovaParser):
info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String instanceDetail = NovaJSON.shared().receiveDetail(getId());
Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.instances_info);
TextView image = (TextView) dialog.findViewById(R.id.imageInstance);
TextView flavor = (TextView) dialog.findViewById(R.id.flavorInstance);
dialog.setTitle(name.getText() + " Details");
if (instanceDetail != null) {
image.setText(" \u2022 image : " + NovaParser.shared().parseImages(instanceDetail));
flavor.setText(" \u2022 flavor : " + NovaParser.shared().parseFlavor(instanceDetail));
}
dialog.show();
}
});
这是对 NovaJSON 类执行 Volley 请求的方法:
public void getJSONdetail() {
final String authToken = getAuth();
String novaURL = getNova();
novaURL = novaURL+"/servers/"+id;
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, novaURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Nova on Response", response.toString());
setNovaJSONdetail(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Nova on Error", "Error: " + error.getMessage());
setNovaJSONdetail(error.toString());
}
}
) {
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("X-Auth-Token", authToken);
params.put("User-Agent", "stackerz");
params.put("Accept", "application/json");
params.put("Content-Type", "application/json; charset=utf-8");
return params;
}
};
queue = VolleySingleton.getInstance(this).getRequestQueue();
queue.add(getRequest);
}
然后它将 JSON 作为字符串从服务器发送,以使用以下方法进行解析:
public static String parseImages(String imagesDetail){
ArrayList<HashMap<String, String>> imagesList = NovaParser.shared().getImagesList();
String temp = null;
JSONObject novaDetail = null;
try {
novaDetail = new JSONObject(imagesDetail);
JSONObject server = novaDetail.getJSONObject("server");
JSONObject image = server.getJSONObject("image");
if (imagesList !=null){
temp = image.getString("id");
for (Map<String,String> map : imagesList) {
if (map.containsValue(temp)) {
temp = map.get(NAME);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return temp;
}
public static String parseFlavor(String instanceDetail){
ArrayList<HashMap<String, String>> flavorList = NovaParser.shared().getFlavorList();
String temp = null;
JSONObject novaDetail = null;
try {
novaDetail = new JSONObject(instanceDetail);
JSONObject server = novaDetail.getJSONObject("server");
JSONObject flavor = server.getJSONObject("flavor");
if (flavorList !=null){
temp = flavor.getString("id");
for (Map<String,String> map : flavorList) {
if (map.containsValue(temp)) {
temp = map.get(NAME);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return temp;
}
当我按下按钮时,一旦对话框显示为空值。当我第二次按下它时,我得到了正确的解析数据。基本上第一次单击按钮时,instanceDetail 字符串为空,因为 Volley 没有完成它的操作,然后我第二次单击它相应地加载值,因为它终于完成了第一个请求。
我知道 Volley 是异步的,请求是并行发生的,响应有时不是即时的,但是我需要某种进度条或旋转轮来向用户提供应用程序正在等待数据的一些反馈。可以使用 AsyncTask 完成,但使用 Volley 似乎不可能。
【问题讨论】:
标签: java android json android-volley