【问题标题】:Posting data to server giving null values将数据发布到服务器提供空值
【发布时间】:2013-08-28 02:21:56
【问题描述】:

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

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

【问题讨论】:

  • 你有没有调试过这些变量在它们尝试发布之前是否有值?

标签: android post null


【解决方案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();
   }
}

【讨论】:

  • 这应该是一个建议而不是答案,因为你只是告诉他记录他得到的错误
  • @kabuto178 :我的回答中没有日志语句。我刚刚给出了调用Http Post请求的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多