Java 如何将带有截图的json发布到php web服务
我试图通过截取将Json发布到我的restful API中,但它不起作用。到目前为止,我已经通过chrome上的AdvanceREST客户端应用程序发送了一个json负载来测试我的web服务,它返回一个json响应。。。。但当我尝试截击时,它会返回一个错误的响应。请有人告诉我如何解决这个问题,提前谢谢 Json有效负载:Java 如何将带有截图的json发布到php web服务,java,php,android,json,android-volley,Java,Php,Android,Json,Android Volley,我试图通过截取将Json发布到我的restful API中,但它不起作用。到目前为止,我已经通过chrome上的AdvanceREST客户端应用程序发送了一个json负载来测试我的web服务,它返回一个json响应。。。。但当我尝试截击时,它会返回一个错误的响应。请有人告诉我如何解决这个问题,提前谢谢 Json有效负载: {"country":"isreal","mobile":"009988778"} 代码 private void processLogin(){ showProgre
{"country":"isreal","mobile":"009988778"}
代码
private void processLogin(){
showProgressDialog();
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
Const.LOGIN_URL,null,新响应.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.i(“JSON响应”、“JSON发布”+response.toString());
HideProgesDialog();
}
},new Response.ErrorListener(){
@凌驾
公共错误响应(截击错误截击错误){
d(ERROR_标记,“ERROR:+volleyError.getMessage());
HideProgesDialog();
}
}){
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json”);
返回标题;
}
@凌驾
受保护的映射getParams(){
字符串country_value=country.getSelectedItem().toString();
字符串mobile_value=mobile.getText().toString();
Map params=新的HashMap();
参数put(“国家”,国家/地区值);
参数put(“移动”,移动值);
返回参数;
}
};
AppController.getInstance().addToRequestQueue(JSONOBJEQ,登录标签);
}
您正在做的是,您试图将JSON作为标题的一部分发送,但这是行不通的
这就是你需要的-
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.LOGIN_URL, **->YOUJSONOBJECTHERE<-**, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("JSON response", "JSON Posting" + response.toString());
hideProgessDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(ERROR_TAG, "Error: " + volleyError.getMessage());
hideProgessDialog();
}
});
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
Const.LOGIN_URL,**->YOUJSONOBJECTHERE您正在做的是,您试图将JSON作为头的一部分发送,但这不起作用
这就是你需要的-
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.LOGIN_URL, **->YOUJSONOBJECTHERE<-**, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("JSON response", "JSON Posting" + response.toString());
hideProgessDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(ERROR_TAG, "Error: " + volleyError.getMessage());
hideProgessDialog();
}
});
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
Const.LOGIN\u URL,**->YOUJSONOBJECTHERE
将对象转换为json字符串
覆盖截击请求的getBody
反对
重写getBodyContentType以设置为json
Gson gson = new Gson();
final String json = gson.toJson(loginDTO);
GsonRequest<EmailLoginRestResponse> jsObjRequest = new GsonRequest<EmailLoginRestResponse>(
Request.Method.POST, url,
EmailLoginRestResponse.class, null,
this.createLoginRequestSuccessListener(),
this.createLoginErrorListener()){
@Override
public byte[] getBody() throws AuthFailureError {
return json.getBytes();
}
@Override
public String getBodyContentType() {
return "application/json; charset=" + this.getParamsEncoding();
}
};
jsObjRequest.setShouldCache(false);
this.mRequestQueue.add(jsObjRequest);
Gson-Gson=new-Gson();
最终字符串json=gson.toJson(loginDTO);
GsonRequest jsObjRequest=新的GsonRequest(
Request.Method.POST,url,
EmailLoginResponse.class,null,
this.createLoginRequestSuccessListener(),
此文件为.createLoginErrorListener()){
@凌驾
公共字节[]getBody()抛出AuthFailureError{
返回json.getBytes();
}
@凌驾
公共字符串getBodyContentType(){
return“application/json;charset=“+this.getParamsEncoding()”;
}
};
jsObjRequest.setShouldCache(false);
this.mRequestQueue.add(jsObjRequest);
GsonRequest.java
将对象转换为json字符串
覆盖截击请求的getBody
反对
重写getBodyContentType以设置为json
Gson gson = new Gson();
final String json = gson.toJson(loginDTO);
GsonRequest<EmailLoginRestResponse> jsObjRequest = new GsonRequest<EmailLoginRestResponse>(
Request.Method.POST, url,
EmailLoginRestResponse.class, null,
this.createLoginRequestSuccessListener(),
this.createLoginErrorListener()){
@Override
public byte[] getBody() throws AuthFailureError {
return json.getBytes();
}
@Override
public String getBodyContentType() {
return "application/json; charset=" + this.getParamsEncoding();
}
};
jsObjRequest.setShouldCache(false);
this.mRequestQueue.add(jsObjRequest);
Gson-Gson=new-Gson();
最终字符串json=gson.toJson(loginDTO);
GsonRequest jsObjRequest=新的GsonRequest(
Request.Method.POST,url,
EmailLoginResponse.class,null,
this.createLoginRequestSuccessListener(),
此文件为.createLoginErrorListener()){
@凌驾
公共字节[]getBody()抛出AuthFailureError{
返回json.getBytes();
}
@凌驾
公共字符串getBodyContentType(){
return“application/json;charset=“+this.getParamsEncoding()”;
}
};
jsObjRequest.setShouldCache(false);
this.mRequestQueue.add(jsObjRequest);
GsonRequest.java
< P> < /P>编辑您的问题,包括在Chrome中通过高级REST客户端工作的普通JSON有效载荷,但不通过截击操作。编辑您的问题,包括在Chrome中通过高级REST客户端工作的普通JSON有效载荷,但不能通过Volley工作。我很高兴,它工作了。请考虑接受答案,这样就可以了。帮助别人。我很高兴,它工作。请考虑接受答案,以便它可以帮助他人。