在我的Android应用程序中,我有一个StringRequest并使用Volley发送它。问题是onResponse和onErrorResponse都没有被调用!!我在我的应用程序的其他部分使用JSONRequest,我完全没有问题。 我错过了什么?
Here is one of the samples I tried
这让我困扰了几个小时,我不知道是什么导致了这一点。任何线索都高度赞赏。
以下是代码:
public class AccountManagerProxy {
StringRequest req;
public void run() {
try {
String s1 = URLEncoder.encode("user", "UTF-8");
String s2 = URLEncoder.encode("pass", "UTF-8");
String uri = String.format("http://****/token?grant_type=password&username=%1$s&password=%2$s", s1, s2);
req = new StringRequest(Request.Method.GET, uri, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
VolleyLog.v("Response:%n %s", response);
Log.d("##", "##");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
Log.d("@@", "@@");
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} Singleton.getInstance().getRequestQueue().add(req);
}
}