[英]Setting parameters in HTTP POST
我正在嘗試使用HttpClient API對服務器進行JSON調用。 代碼sinppet如下所示。
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpPost(URLString);
HttpResponse response = httpClient.execute(httpPost);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("method", "completeUserLogin"));
String[] params = new String[]{"100408"};
response = httpClient.execute(httpPost);
我想將參數添加到nameValuePairs。 BasicNameValuePair類不允許您添加數組。 有什么想法嗎?
提前致謝!
看這個 。 它們在BasicNameValuePairs中傳遞數組。這里的顏色是我們要在服務器上發送的數組。 您應該使用數組變量而不是顏色。
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("colours[0]","red"));
nameValuePairs.add(new BasicNameValuePair("colours[1]","white"));
nameValuePairs.add(new BasicNameValuePair("colours[2]","black"));
nameValuePairs.add(new BasicNameValuePair("colours[3]","brown"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);
如果您以json格式發布數據,則不應發布此類參數。 而是創建一個JSONObject,將這些值放在該json對象中,並從該json對象獲取一個字符串,然后創建一個StringEntity,並將此Entity設置為HttpPost對象。
為請求創建JSONObject:
JSONObject json=new JSONObject();
json.put("method", "completeUserLogin");
JSONArray arr= new JSONArray();
arr.put("100408");
json.put("params", arr);
String params=json.toString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.