我正在使用Volley但是我对JSON解析数据有一些问题很可能是因为凌空没有实现像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字符串为null,因为Volley没有完成它的事情,然后我点击第二次它相应地加载值,因为它最终完成了第一个请求。
我理解Volley是异步的,请求是并行发生的,并且响应有时并不是立即的,但是我需要某种进度条或旋转轮来向用户提供应用程序正在等待数据的一些反馈。可以使用AsyncTask来完成它,但是对于Volley来说它似乎无法实现。
答案 0 :(得分:0)
我认为你的问题不是因为Volley
检查您发送和接收的参数
但是如果你需要onPostExcecute
,你就有Volley的回调:
在请求后调用Response.Listener<JSONObject>
和Response.ErrorListener()
。
关于Volley request manager
只需使用适当的Volley request manager
来电
答案 1 :(得分:0)
我通过完全倾倒Volley并转向Retrofit解决了我的问题。我将所有调用设置为同步/阻塞,使用try / catches计算出异常/错误,并在OkHTTP客户端上设置短暂超时。现在它按照我的意愿工作。