2012-09-11 2 views
0

HttpClient API를 사용하여 서버에 JSON 호출을 시도하고 있습니다. code sinppet은 아래와 같습니다.HTTP POST의 매개 변수 설정

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에 params를 추가하고 싶습니다. BasicNameValuePair 클래스는 배열을 추가 할 수 없습니다. 이견있는 사람?

미리 감사드립니다.

+0

당신은 그것을 추가 한 후 우편으로 예상 문자열 표현을 배열로 변환 시도하고 있습니다. – CasualT

+0

알다시피, 그냥 jeet의 대답을보십시오. :) – CasualT

답변

1

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(); 
6

이것을보십시오. 여기서 그들은 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); 
+0

감사합니다! 너 나 갈거야. – Renjith