-1

嗨,我想将 JSON 发布到 PHP 脚本。 这是代码:

JSONObject obj = new JSONObject();
for(int k = 0; k<len;k++){
   obj.put("nachrichten_ids", params[k]);
}               
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("xxxxxx");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("nachrichten_ids",obj.toString()));

httpPost.setHeader("Content-type", "application/json");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);   

PHP 脚本实际上只包含 print_r($_POST),并且每次都是空的。

问题出在哪里 ?

Log.i("TEST",obj.toString()) 输出: {"nachrichten_ids":"[2144,2138]"}

其中每个数字都是应删除的消息的 IDS。

4

2 回答 2

0

这个对我有用 :

/**
     * Functionality of HTTP POST and return response
     * @param url to hit
     * @param json object to post
     * 
     * @return string response
    */
        public String httpPostWithJson(String url, JSONObject json)
        {
            String result = "";
            try
            {
                 HttpPost httppost = new HttpPost(url);

                HttpParams myParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(myParams, 10000);
                HttpConnectionParams.setSoTimeout(myParams, 60000);
                HttpConnectionParams.setTcpNoDelay(myParams, true);

                httppost.setHeader("Content-type", "application/json");
                HttpClient httpclient = new DefaultHttpClient(myParams);


               StringEntity se = new StringEntity(json.toString(), HTTP.UTF_8); 
               se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
               httppost.setEntity(se); 

              // Execute HTTP Post Request
               HttpResponse response = httpclient.execute(httppost);
    //        Log.e("","response code = "+response.getStatusLine().getStatusCode() );
              if (response.getStatusLine().getStatusCode() == 200)
              {
                  HttpEntity entity = response.getEntity();
                  result = EntityUtils.toString(entity);
              }
            }
            catch (Exception e) {
    //          Log.e(TAG, e.toString());
            }
            return result;
        }
于 2013-01-21T09:41:43.140 回答
-1

所以,现在它的工作。我使用了库“Gson”:

    final ArrayList<String> delMessagesId = new ArrayList<String>(); 
    for(int i=0;i<nachrichtenListe.size();i++){
      Nachricht nachricht = nachrichtenListe.get(i);
      if(nachricht.isSelected() == true){
        delMessagesId.add(nachricht.nachricht_id); 
       }
    }

    String ddate = new Gson().toJson(params);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("xxx");

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("nachrichten_ids", ddate));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    // Get the return string (JSON data)
    String temp = EntityUtils.toString(entity);
    Log.i("TEST",temp);
于 2013-01-21T16:48:18.703 回答