Android 带有原始数据的截击请求
在我的应用程序中,我必须使用JSON req参数发送POST请求,我尝试使用Postman Rest客户端创建请求,它工作正常,但无法使用下面的代码 在Postman req中,参数作为原始数据发送,但我不确定如何通过截取请求发送它Android 带有原始数据的截击请求,android,android-volley,androidhttpclient,Android,Android Volley,Androidhttpclient,在我的应用程序中,我必须使用JSON req参数发送POST请求,我尝试使用Postman Rest客户端创建请求,它工作正常,但无法使用下面的代码 在Postman req中,参数作为原始数据发送,但我不确定如何通过截取请求发送它 public Request getHTTPPostReqResponse(String URL, Class mClass, final Map<String, String> params, final String contentType, fin
public Request getHTTPPostReqResponse(String URL, Class mClass, final Map<String, String> params, final String contentType, final String body) {
mResponseListener.requestStarted();
Request mRequest = new GsonRequest(Request.Method.POST, URL, mClass, new Response.Listener<Object>() {
@Override
public void onResponse(Object response) {
mResponseListener.requestCompleted(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mResponseListener.requestEndedWithError(error);
}
}) {
@Override
protected Map<String, String> getParams() {
return params;
}
@Override
public byte[] getBody() throws AuthFailureError {
if (TextUtils.isEmpty(body)){
return super.getBody();
}else {
return body.getBytes();
}
}
@Override
public String getBodyContentType() {
return contentType;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", contentType);
return params;
}
};
mRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
return mRequest;
}
希望这不会太晚 您是否尝试过其他类型的请求,如String或JsonObject? 参数有不同的语法吗 e、 g
Map jsonParams=newarraymap();
jsonParams.put(“nodeId”,null);
jsonParams.put(“userId”,null);
jsonParams.put(“mobileNumber”sharma@gmail.com");
jsonParams.put(“userProfile”,null);
jsonParams.put(“区域”,null);
jsonParams.put(“国家代码”,91);
jsonParams.put(“密码”,pass@123);
put(“places”,newarraylist());
put(“trustedNetwork”,newArrayList());
jsonParams.put(“profilePic”,null);
jsonParams.put(“全名”,null);
jsonParams.put(“经度”,0.0);
jsonParams.put(“纬度”,0.0);
JsonObjectRequest=新的JsonObjectRequest(request.Method.POST、url、新的JSONObject(jsonParams),
新的Response.Listener()
{
@凌驾
公共void onResponse(JSONObject响应)
{
mResponseListener.requestCompleted(响应);
}
},
新的Response.ErrorListener()
{
@凌驾
公共无效onErrorResponse(截击错误)
{
if(null!=错误。networkResponse)
{
mResponseListener.requestendWithError(错误);
}
}
});
还有,看一看。
希望这些都有帮助。这是工作代码,您可以试试。我们可以对原始数据使用JsonObjectRequest。这里我刚刚演示了如何创建添加到队列中的requestObject
String url = MConstant.API_END+"/userLogin";
Map<String, String> params = new HashMap<String, String>();
params.put("email",mEmailView.getText().toString());
params.put("password",mPasswordView.getText().toString());
params.put("api",MConstant.API);
JSONObject objRegData = new JSONObject(params);
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.POST, url, objRegData, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// handle response data
} catch (JSONException e) {
e.printStackTrace();
//pDialog.show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
你能更准确地描述一下这个问题吗?你能把你向邮递员提出的要求寄出去吗?
Map<String, Object> jsonParams = new ArrayMap<>();
jsonParams.put("nodeId", null);
jsonParams.put("userId", null);
jsonParams.put("mobileNumber", "sharma@gmail.com");
jsonParams.put("userProfile", null);
jsonParams.put("region", null);
jsonParams.put("countryCode", 91);
jsonParams.put("password", pass@123);
jsonParams.put("places", new ArrayList());
jsonParams.put("trustedNetwork", new ArrayList());
jsonParams.put("profilePic", null);
jsonParams.put("fullName", null);
jsonParams.put("longitude", 0.0);
jsonParams.put("latitude", 0.0);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams),
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
mResponseListener.requestCompleted(response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
if (null != error.networkResponse)
{
mResponseListener.requestEndedWithError(error);
}
}
});
String url = MConstant.API_END+"/userLogin";
Map<String, String> params = new HashMap<String, String>();
params.put("email",mEmailView.getText().toString());
params.put("password",mPasswordView.getText().toString());
params.put("api",MConstant.API);
JSONObject objRegData = new JSONObject(params);
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.POST, url, objRegData, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// handle response data
} catch (JSONException e) {
e.printStackTrace();
//pDialog.show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
$json = file_get_contents('php://input');
$myPostData = json_decode($json, true);