[英]How to send JSON data to the server in Android
我想將json數據發送到服務器,但無法執行。 我在下面粘貼了json數據的有效負載,請檢查並幫助我。 在我的Json Data有效負載中,如此多的數組和對象給我造成了問題,從而發送服務器。 我已經檢查了所有在谷歌的職位,但不能做到這一點。
//payload
{
"action":"create",
"machinetypelist":[{"id":"","materialTypeId":"1","machineinplantid":"MIPID-103","material":["1","2"]}]
}
您可以使用Gson
從模型發送json數據。
//escape the double quotes in json string
String payload="{\"action\":\"create\",\"machinetypelist\":[{\"id\":\"\",\"materialTypeId\":\"1\",\"machineinplantid\":\"MIPID-103\",\"material\":[\"1\",\"2\"]}]}"
String requestUrl="your url";
sendPostRequest(requestUrl, payload);
創建sendPostRequest
方法。 這將起作用。 我引用了此鏈接
This is the sollution for my question-
JSONObject js = new JSONObject ();
try {
js.put ("action","create");
JSONObject jsonObject = new JSONObject ();
jsonObject.put ("id","");
jsonObject.put ("materialTypeId","");
jsonObject.put ("machineinplantid","");
JSONArray jsonArray = new JSONArray ();
jsonArray.put ();
jsonObject.put ("material",jsonArray);
JSONArray jsonArray1 = new JSONArray ();
jsonArray1.put (jsonObject);
js.put ("machinetypelist",jsonArray1);
} catch (JSONException e) {
e.printStackTrace ( );
}
通過使用Volley,您可以按如下所示的請求發送JSON
數據,希望這將幫助您了解如何發送JsonObjectRequest
。
private void sendJsonData(JSONObject jsonObjectRequest) {
String url = "your_url";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObjectRequest,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Log.d("TAG", "onResponse: get your response here ");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("TAG", "onErrorResponse: ERROR");
}
}) {
};
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.