这是我的HttpURLConnection请求正常工作。 我发送json String请求 现在我必须将此请求转换为齐射
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;
}
我已经针对相同的请求编写了这段代码
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请求中的位置 使用String请求它给出400错误(错误请求) 使用JSonObject Request它会产生500错误
在Http URL Connection中,每个东西都运行良好,jsonStr正在传递
OutputStream os = urlConnection.getOutputStream();
os.write(jsonStr.getBytes());
os.flush();
os.close();
答案 0 :(得分:0)
这是我解决问题的方法 在事件HttpClient请求中使用的StringEntity的创建对象
<script>
然后覆盖方法
StringEntity entity = new StringEntity(JsonStr);
所以完整的答案将是
@Override
public String getBodyContentType() {
return entity.getContentType().getValue();
}
@Override
public byte[] getBody() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
entity.writeTo(outputStream);
} catch (IOException e) {
VolleyLog.e("IOException @ " + getClass().getSimpleName());
}
return outputStream.toByteArray();
}