您好我正在使用以下代码将数据发布到服务器,但它将空值发布到服务器。任何人都可以告诉我这段代码有什么问题吗?
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new 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
}
}
答案 0 :(得分: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();
}
}