我正在尝试发布一个json数组(listobjs):
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", listobjs.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = client.execute(httppost);
我收到服务器响应:
11-23 13:14:23.873: E/WebServicesManager(9059): response =500
11-23 13:14:23.873: E/WebServicesManager(9059): response ={"error":{"message":"data is required","type":"Api_Exception","code":201}}
为什么服务器没有获取我的数据JSONArray?
也许这会有所帮助,这就是我在iphone上的表现:
data=[{"scan_user":"2289","scan_status":"2","scan_date":"2012-10-23 10:29:53","id_participant":"2969113"},{"scan_user":"2280","scan_status":"2","scan_date":"2012-10-23 10:40:53","id_participant":"2969112"}]
NSString * stringToPost = [NSString stringWithFormat: @"data=%@", [participantsListToPost JSONRepresentation]];
答案 0 :(得分:0)
不是将listobjs
作为单个string
发送,而是循环每个对象并使用NameValue
对发送数据。
答案 1 :(得分:0)
您必须使用HTTP POST
请求。这就是错误消息告诉你的内容。
试试这个
HttpParams parameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(parameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(parameters, SOCKET_TIMEOUT);
HttpClient httpClient = new DefaultHttpClient(parameters);
try {
HttpPost request = new HttpPost(url);
StringEntity jsonEntity = new StringEntity(data, "UTF8");
jsonEntity.setContentType("application/json");
request.setEntity(jsonEntity);
HttpResponse httpResponse = mHttpClient.execute(request);
// Get the response and do anything…
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
public void clickbutton(View v) {
try {
// http://androidarabia.net/quran4android/phpserver/connecttoserver.php
// Log.i(getClass().getSimpleName(), "send task - start");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
//
HttpParams p = new BasicHttpParams();
// p.setParameter("name", pvo.getName());
p.setParameter("user", "1");
// Instantiate an HttpClient
HttpClient httpclient = new DefaultHttpClient(p);
String url = "http://10.0.2.2:8080/sample1/" +
"webservice1.php?user=1&format=json";
HttpPost httppost = new HttpPost(url);
// Instantiate a GET HTTP method
try {
Log.i(getClass().getSimpleName(), "send task - start");
//
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("user", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,
responseHandler);
// Parse
JSONObject json = new JSONObject(responseBody);
JSONArray jArray = json.getJSONArray("posts");
ArrayList<HashMap<String, String>> mylist =
new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jArray.getJSONObject(i);
String s = e.getString("post");
JSONObject jObject = new JSONObject(s);
map.put("idusers", jObject.getString("idusers"));
map.put("UserName", jObject.getString("UserName"));
map.put("FullName", jObject.getString("FullName"));
mylist.add(map);
}
Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Log.i(getClass().getSimpleName(), "send task - end");
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}
另请参阅此链接。 http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php
希望对你有所帮助。