gpt4 book ai didi

android - 将数据发布到服务器提供空值

转载 作者:行者123 更新时间:2023-11-30 03:24:30 24 4
gpt4 key购买 nike

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

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
}
}

最佳答案

为您的 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();
}
}

关于android - 将数据发布到服务器提供空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18472622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com