我正在使用volley包处理Gson,但是它会抛出错误。我的网址还可以。它在浏览器上给我json值但在方法中它抛出空指针异常。从stringRequest行直接进入try块并返回null值,如“0x239c9b75 NORMAL null”
请帮我解决这个问题。
public void getJobs() {
// mProgressBar.setVisibility(View.VISIBLE);
//for open jobs Client/GetOpenJobs
StringRequest request = new StringRequest(Request.Method.GET, RestAPI.getInstance().urlString+ "Getrewardlist?businessid="+1,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
getRewardList = (new Gson()).fromJson(response, GetRewardList.class);
try {
if (ResponseCode == 200) {
RewardListEntity[] Items = getRewardList.entitiesList;
if(Items!=null&& Items.length>0) {
Utilities.R_entities = Arrays.asList(Items);
mProgressBar.setVisibility(View.GONE);
return;
}else{
mProgressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "No jobs exists", Toast.LENGTH_SHORT).show();
return;
}
} else {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.GONE);
}
} catch (Exception e) {
String ResponseMessage = e.getMessage();
Toast.makeText(MainActivity.this, ResponseMessage, Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.GONE);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyError e = parseNetworkError(error);
Toast.makeText(MainActivity.this, e + "", Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.GONE);
return;
}
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
}
return volleyError;
}
}
) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
// params.put("SessionToken", RestAPI.getInstance().sessionToken);
return params;
}
};
try {
request.setRetryPolicy(new DefaultRetryPolicy(60000, 60, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleyApplication.getInstance().getRequestQueue().add(request);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
首先尝试打印出响应
Log.d("",response);
查看它是否在回调中实际收到,如果是 - 问题可能就是你用Gson解析响应的方式。
希望这有帮助!