总共有两种情形:
1:针对stringRequest
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(address,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if ("countyCode".equals(type)) {
if (!TextUtils.isEmpty(response)) {
String[] array = response.split("\\|");
if (array != null && array.length == 2) {
String weatherCode = array[1];
LogUtil.i("WeatherActivity",
"-------------->" + weatherCode);
queryWeatherInfo(weatherCode);
}
}
} else if ("weatherCode".equals(type)) {
// 处理服务器返回的天气信息
Utility.handleWeatherResponse(
getApplicationContext(), response);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
showWeather();
}
});
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
publishText.setText("同步失败");
}
});
}
})
{
protected Response<String> parseNetworkResponse(NetworkResponse response){
try {
String string = new String(response.data, "UTF-8");
return Response.success(string,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (Exception je) {
return Response.error(new ParseError(je));
}
}
};
mQueue.add(stringRequest);
2:针对objRequest
JsonObjectRequest objRequest = new JsonObjectRequest(Request.Method.GET,url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject obj) {
Log.d("------", obj.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.getMessage();
}
})
{
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response)
{
JSONObject jsonObject;
try {
jsonObject = new JSONObject(new String(response.data,"UTF-8"));
return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
} catch (JSONException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
};
queue.add(objRequest);