1

您好我正在使用以下代码将数据发布到服务器,但它将空值发布到服务器。谁能告诉我这段代码有什么问题?

HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = 新的 HttpPost(url);

    Log.i("name",name);
    Log.i("email",email);
    Log.i("subject",subject);
    Log.i("mobile",mobile);
    Log.i("url",url);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);

        nameValuePairs.add(new BasicNameValuePair("name", name));
        nameValuePairs.add(new BasicNameValuePair("email", email));
        nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
        nameValuePairs.add(new BasicNameValuePair("subject", subject));
        nameValuePairs.add(new BasicNameValuePair("message", message));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity=response.getEntity();
      //  Toast.makeText(getApplicationContext(), response + "", Toast.LENGTH_LONG).show();
        String responseText = EntityUtils.toString(entity);

        Log.i("Response",responseText + "");
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 
4

1 回答 1

1

为您的 HTTP Post 请求尝试以下代码:

HttpPost post = new HttpPost(url);
HttpClient hc = new DefaultHttpClient();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
nameValuePairs.add(new BasicNameValuePair("subject", subject));
nameValuePairs.add(new BasicNameValuePair("message", message));

 try {
     post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     post.getParams().setBooleanParameter("http.protocol.expect-continue", false);
     HttpResponse rp = hc.execute(post);
   } catch (Exception e) {
     // TODO Auto-generated catch block
    e.printStackTrace();
   }
}
于 2013-08-27T18:21:56.947 回答