0

我正在尝试使用以下代码在服务器上发布数据,但数据是针对一个字段发布的,只有其他字段发布为空。我的代码有什么问题。我需要在我的 android 应用程序中做任何其他更改吗?

public void postData(String name,String email,String mobile,String subject,String          message,String url)
{
    // Create a new HttpClient and Post Header

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

    try {
        // Add your data
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);

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

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity=response.getEntity();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

我的代码有什么问题?

4

2 回答 2

0

我不能说你传递 nameValuePairList 是绝对错误的,但我传递给 httpPost.setEntity() 的对象是 MultipartEntity。

MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("KEY", new StringBody("some string"));
reqEntity.addPart("ANOTHER_KEY", new StringBody("another string"));
httpPost.setEntity(reqEntity)

MultipartEntity 是 Apache Http 组件的一部分

http://hc.apache.org/downloads.cgi

http 客户端下载具有包含该类的 httpmime-4.xxjar。您只需要 httpmime jar,其余的不需要。

于 2013-09-15T06:06:15.320 回答
0
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);

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

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

你的代码对我来说很好。检查是否subject不为空。
因为你已经用subject!

于 2013-09-15T07:03:45.803 回答