-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

それで、今それは働いています。lib「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 に答える