【发布时间】:2015-11-25 11:34:29
【问题描述】:
这是我的 HttpURLConnection 请求,它工作正常。 我正在发送带有请求的 json 字符串 现在我必须将此请求转换为 volley
String jsonStr = {"email":"test@gmail.com","full_name":"vghjj",
"locations":[],"mobile_no":"XXXXXXXXX",
"super_sectors": [],"unique_device_id":"XXXXXXX","utm_params":"No Utm Params"}"
try {
HttpURLConnection urlConnection = getHttpConnection(jsonStr, url, oauth, REQUEST_TYPE.POST.getType());
if (urlConnection != null) {
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
result = convertInputStreamToString(urlConnection.getInputStream());
}
}
} catch (Exception e) {
return null;
}
private static HttpURLConnection getHttpConnection(String jsonStr , String strUrl, Oauth oauth, String type) {
URL url = null;
HttpURLConnection urlConnection = null;
try {
url = new URL(strUrl);
/* */
if (GlobalVariables.DEVELOPING)
Log.v(TAG, url.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(type);
urlConnection.setConnectTimeout(GlobalVariables.applyInternetConnectionTimeOut);
urlConnection.setReadTimeout(GlobalVariables.applyInternetSocketTimeOut);
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("Content-type", "application/json");
urlConnection.setRequestProperty("Authorization", oauth.getToken_type() + " " + oauth.getAccess_token());
OutputStream os = urlConnection.getOutputStream();
os.write(jsonStr.getBytes());
os.flush();
os.close();
return urlConnection;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我已经为同一个请求在 volley 中编写了这段代码
StringRequest= new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Successfull Stuff
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: " + error.toString());
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("allValue", jsonStr);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("Accept", "application/json");
params.put("Authorization", oauth.getToken_type() + " " + oauth.getAccess_token());
return params;
}
};
/*Generate Queue to line up apply request*/
RequestQueue requestQueue = VollySingleton.getsInstance().getmRequestQueue();
requestQueue.add(stringRequest);
所以我的具体问题是 jsonStr 在 Volley 请求中去哪里 使用字符串请求它给出 400 错误(错误请求) 使用 JSONObject 请求它给出 500 错误
在 Http URL Connection 中,一切正常,jsonStr 正在传入
OutputStream os = urlConnection.getOutputStream();
os.write(jsonStr.getBytes());
os.flush();
os.close();
【问题讨论】:
-
如果您尝试从某个服务器获取
JSON数据,请使用JsonObjectRequest。接下来,您正在使用的代码不适用于硬编码的 JSON 字符串。您的代码正在尝试通过您提到的 URL 从服务器中提取 JSON 数据。查看本教程,您将了解volley的工作原理。 (code.tutsplus.com/tutorials/…) -
我阅读了文档,但从未发现任何关于 jsonStr 的请求,在 HttpURLConnection 中,它们是 OutputStream 的选项,我问过的那个选项或 volley 中的类似选项在哪里。
-
public void onResponse(String response){...}-response与您的jsonStr相似。
标签: android android-volley httpurlconnection