whats upAndroid Volley POST、JSONObject(int、string、jsonobjects)
私のサーバーJSONObjectに投稿しようとしています。
は私がスタックにあるいくつかのコードを試みた:私は取得しています
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();
}
をエラー:
E/Volley: [275] BasicNetwork.performRequest: Unexpected response code 400 for http://10.0.2.2:8080/order 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);
エラー:
E/Volley: [275] BasicNetwork.performRequest: Unexpected response code 400 for http://10.0.2.2:8080/order
2番目の記事では、「Content-Type」というヒントを見つけましたが、何も変わりませんでした。
{ "waiterId":3、 "tableNumber":3、 "備考": "orderRemark"、 "製品":[]
以前私はこのようなポストマンを使用してオブジェクトを追加しました}
問題点は何ですか?おそらく、Imは「製品」をひどく追加しています。複数の「製品」を追加する方法、または「製品」なしでjsonを投稿する方法
ありがとうございます!
EDIT: jsonObject = new JSONObject( "{\" waiterId \ ":3、\" tableNumber \ ":3、\" remark \ ":\" orderRemark \ "、\" products \ \ ":[]}");今はコード400の問題がありません。新しい要素を投稿するだけではありません。エラーメッセージは表示されません。
EDIT 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;
}
リンクが奨励されて動作しますが、それはありますなぜあなたの仲間のユーザーはそれが何であるか、いくつかのアイデアを持っているとしますので、リンク周りのコンテキストを追加してください。ターゲットサイトに到達できない場合や、永続的にオフラインになる場合は、常に重要なリンクの最も関連性の高い部分を引用してください。 – Shashanth