Android 如何将HttpURL连接请求转换为批量请求??
这是我的HttpURLConnection请求,它工作正常。 我正在发送带有请求的json字符串 现在我必须把这个请求转换成截击Android 如何将HttpURL连接请求转换为批量请求??,android,android-volley,httpurlconnection,Android,Android Volley,Httpurlconnection,这是我的HttpURLConnection请求,它工作正常。 我正在发送带有请求的json字符串 现在我必须把这个请求转换成截击 String jsonStr = {"email":"test@gmail.com","full_name":"vghjj", "locations":[],"mobile_no":"XXXXXXXXX", "super_sectors": [],"unique_device_
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);
我就是这样解决这个问题的 已创建StringEntity的对象,该对象将在HttpClient请求事件中使用
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();
}
因此,完整的答案将是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
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();
}
@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);
StringRequest=新的StringRequest(Request.Method.POST,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//成功的东西
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.d(标记“onErrorResponse:+error.toString());
}
}
) {
@凌驾
公共字符串getBodyContentType(){
返回实体.getContentType().getValue();
}
@凌驾
公共字节[]getBody(){
ByteArrayOutputStream outputStream=新建ByteArrayOutputStream();
试一试{
entity.writeTo(输出流);
}捕获(IOE异常){
e(“IOException@”+getClass().getSimpleName());
}
返回outputStream.toByteArray();
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
参数put(“接受”、“应用程序/json”);
params.put(“Authorization”,oauth.getToken_type()+“”+oauth.getAccess_token());
返回参数;
}
};
/*生成队列以排列应用请求*/
RequestQueue RequestQueue=VollySingleton.getsInstance().getmRequestQueue();
添加(stringRequest);
我就是这样解决这个问题的
已创建StringEntity的对象,该对象将在HttpClient请求事件中使用 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();
}
因此,完整的答案将是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
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();
}
@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);
StringRequest=新的StringRequest(Request.Method.POST,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//成功的东西
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.d(标记“onErrorResponse:+error.toString());
}
}
) {
@凌驾
公共字符串getBodyContentType(){
返回实体.getContentType().getValue();
}
@凌驾
公共字节[]getBody(){
ByteArrayOutputStream outputStream=新建ByteArrayOutputStream();
试一试{
entity.writeTo(输出流);
}捕获(IOE异常){
e(“IOException@”+getClass().getSimpleName());
}
返回outputStream.toByteArray();
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
参数put(“接受”、“应用程序/json”);
params.put(“Authorization”,oauth.getToken_type()+“”+oauth.getAccess_token());
返回参数;
}
};
/*生成队列以排列应用请求*/
RequestQueue RequestQueue=VollySingleton.getsInstance().getmRequestQueue();
添加(stringRequest);
如果您试图从某个服务器获取JSON
数据,请使用JsonObjectRequest
。接下来是您正在使用的代码,它不适用于硬编码的JSON字符串。您的代码试图通过您提到的URL从服务器中提取JSON数据。看看本教程,您将了解volley
的工作原理。()我阅读了文档,但在HttpURLConnection中没有找到任何关于jsonStr和request的信息,他们的选项是OutputStream,其中该选项或类似的选项与我所问的相同。public void onResponse(String response){…}
-响应
与您的jsonStr
类似。如果您试图从某个服务器获取JSON
数据,请使用JsonObjectRequest
。接下来是您正在使用的代码,它不适用于硬编码的JSON字符串。您的代码试图通过您提到的URL从服务器中提取JSON数据。看看本教程,您将了解volley
的工作原理。()我阅读了文档,但在HttpURLConnection中没有找到关于jsonStr的任何内容,他们的选项是OutputStream,其中该选项或类似选项与我所问的相同。public void onResponse(String response){…}
-response
与您的jsonStr
类似。