我尝试向Web服务发出http登录请求。我设置了JSONobject请求,但是当我尝试调试代码时,响应JSON对象似乎为null。 (我应该得到一个JSON对象的响应)。有什么想法吗?
public UserData initLogin(Creds cr) {
StringBuilder finalURL = new StringBuilder("https://");
finalURL.append(credentials.getUrl());
finalURL.append("/s1services");
Response.Listener<JSONObject> r1 = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
res = response.toString();
us = gson.fromJson(String.valueOf(response), UserData.class);
}
};
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, finalURL.toString(), cr.serObj(), r1, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
return us;
}
我应该将此请求发送到请求队列还是什么?我应该提到凭证类有一个URL属性和一个getUrl()方法。
答案 0 :(得分:0)
我会按照Android文档中的示例进行操作:
StringBuilder finalURL = new StringBuilder("https://");
finalURL.append(credentials.getUrl());
finalURL.append("/s1services");
String url = finalURL.toString();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//handle your json response here
us = gson.fromJson(String.valueOf(response), UserData.class);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);