Android凌空发布带有int、string和jsonobjects的JSONObject
怎么了 我正在尝试在我的服务器JSONObject上发布 我尝试了在stack上找到的一些代码:Android凌空发布带有int、string和jsonobjects的JSONObject,android,json,android-volley,Android,Json,Android Volley,怎么了 我正在尝试在我的服务器JSONObject上发布 我尝试了在stack上找到的一些代码: String url = "http://10.0.2.2:8080/order"; try { RequestQueue requestQueue = Volley.newRequestQueue(this); String URL = "http://10.0.2.2:8080/order"; JSONObject json
String url = "http://10.0.2.2:8080/order";
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://10.0.2.2:8080/order";
JSONObject jsonBody = new JSONObject();
jsonBody.put("waiterId", 1);
jsonBody.put("tableNumber", 4);
jsonBody.put("remark", "asd");
jsonBody.put("products", new JSONObject());
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
stringurl=”http://10.0.2.2:8080/order";
试一试{
RequestQueue RequestQueue=Volley.newRequestQueue(this);
字符串URL=”http://10.0.2.2:8080/order";
JSONObject jsonBody=新的JSONObject();
jsonBody.put(“waiterId”,1);
jsonBody.put(“表号”,4);
jsonBody.put(“备注”、“asd”);
put(“产品”,新的JSONObject());
最后一个字符串requestBody=jsonBody.toString();
StringRequest StringRequest=new StringRequest(Request.Method.POST,URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.i(“截击”,回应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“截击”,error.toString());
}
}) {
@凌驾
公共字符串getBodyContentType(){
返回“application/json;charset=utf-8”;
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
试一试{
return requestBody==null?null:requestBody.getBytes(“utf-8”);
}捕获(不支持的编码异常uee){
wtf(“尝试使用%s获取%s的字节时不支持编码”,请求体,“utf-8”);
返回null;
}
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
字符串responseString=“”;
if(响应!=null){
responseString=String.valueOf(response.statusCode);
//可以获取更多详细信息,如response.headers
}
返回Response.success(responseString,HttpHeaderParser.parseCacheHeaders(Response));
}
};
添加(stringRequest);
}捕获(JSONException e){
e、 printStackTrace();
}
我得到一个错误:
E/截击:[275]基本网络。性能请求:对的意外响应代码400
E/VOLLEY:com.android.VOLLEY.ServerError
或者这个:
String url = "http://10.0.2.2:8080/order";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("waiterId", 1);
jsonObject.put("tableNumber", 1);
jsonObject.put("remark", "zamowienie");
jsonObject.put("products", new JSONObject());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// As of f605da3 the following should work
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Now you can use any deserializer to make sense of data
JSONObject obj = new JSONObject(res);
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
} catch (JSONException e2) {
// returned data is not JSONObject?
e2.printStackTrace();
}
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
Singleton.getInstance(this).addToRequestQueue(jsonObjReq);
stringurl=”http://10.0.2.2:8080/order";
JSONObject JSONObject=新的JSONObject();
试一试{
jsonObject.put(“waiterId”,1);
jsonObject.put(“表号”,1);
jsonObject.put(“备注”,“Zamovienie”);
put(“产品”,新的jsonObject());
}捕获(JSONException e){
e、 printStackTrace();
}
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(
Request.Method.POST、url、jsonObject、,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//自f605da3起,以下各项应起作用
NetworkResponse=错误。NetworkResponse;
if(error instanceof ServerError&&response!=null){
试一试{
String res=新字符串(response.data,
HttpHeaderParser.parseCharset(response.headers,“utf-8”);
//现在,您可以使用任何反序列化程序来理解数据
JSONObject obj=新的JSONObject(res);
}捕获(不支持DencodingException e1){
//无法将数据正确解码为字符串
e1.printStackTrace();
}捕获(JSONException e2){
//返回的数据不是JSONObject?
e2.printStackTrace();
}
}
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
返回标题;
}
};
Singleton.getInstance(this.addToRequestQueue(jsonObjReq);
错误:
E/截击:[275]基本网络。性能请求:对的意外响应代码400
在第二篇文章中,我找到了使用“内容类型”的技巧,但没有任何改变
前面我使用Postman添加了对象,如下所示:
{
“侍者”:3,
“表号”:3,
“备注”:“订单备注”,
“产品”:[]
}
有什么问题?也许我添加的“产品”很糟糕。如何添加多个“产品”或在没有“产品”的情况下发布json
谢谢
编辑:
我尝试将JSONObject更改为JSONObject=newJSONObject(“{\'waiterId\”:3,\'tableNumber\”:3,\'remark\:\'ordermark\”,\'products\:[]}”);。现在我没有代码400的问题。它只是没有发布新元素。它不显示任何错误消息:<(带有两个代码)
编辑2:
我试过其他方法。它不起作用
class AsyncT extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Log.d("A","1");
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("waiterId", 1);
Log.d("A","2");
jsonObject.put("tableNumber", 1);
jsonObject.put("remark", "zamowienie");
jsonObject.put("products", new JSONObject());
} catch (JSONException e) {
e.printStackTrace();
}
URL url = new URL("http://10.0.2.2:8080/order");
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
urlConn = url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");
urlConn.setRequestProperty("Host", "android.schoolportal.gr");
urlConn.connect();
printout = new DataOutputStream(urlConn.getOutputStream ());
printout.writeBytes(URLEncoder.encode(jsonObject.toString(),"UTF-8"));
printout.flush ();
printout.close ();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
类AsyncT扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
日志d(“A”、“1”);
JSONObject JSONObject=新的JSONObject();
试一试{
jsonObject.put(“waiterId”,1);
日志d(“A”、“2”);
jsonObject.put(“表号”,1);
jsonObject.put(“备注”,“Zamovienie”);
put(“产品”,新的jsonObject());
}捕获(JSONException e){
e、 printStackTrace();
}
URL=新URL(“http://10.0.2.2:8080/order");
urlConn;
数据输出流打印输出;
数据输入流输入;
urlConn=url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(真);