1

我正在尝试发布一个 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]];
4

3 回答 3

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

希望对你有所帮助。

于 2012-10-23T11:42:27.510 回答
0

不是listobjs作为单个发送,而是string循环每个对象并使用NameValuePair 发送数据。

于 2012-10-23T11:30:24.260 回答
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();
}
于 2012-10-23T11:31:50.240 回答