Android http发送不发送数据

问题描述:

首先,我收到状态码200.在php脚本中,我只是让代码发送给我什么被发送到服务器(当我尝试和调试时)。使用var_dump $ _POST或$ _REQUEST我收到一封空白邮件。如果我在$ _SERVER上运行一个foreach,我得到一些像HTTP_USER_AGENT的数据:Apache-HttpClient/UNAVAILABLE(java 1.4)和CONTENT_LENGTH:9Android http发送不发送数据

我不明白为什么没有发布数据?我是新来的机器人,我从字面上学习,因为我去。

HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(dest_url); 
    //httppost.setHeader("Accept", "application/json"); 
    //httppost.setHeader("Content-type", "application/json"); 

//JSONObject json = new JSONObject(); 

     // Add your data 
     //json.put("action", "login"); 
     //json.put("username", "kermit"); 
     //json.put("password", "123456"); 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    //nameValuePairs.add(new BasicNameValuePair("myjson", json.toString())); 
    nameValuePairs.add(new BasicNameValuePair("plswork", "hi")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 

    //StringEntity str_json = new StringEntity(json.toString()); 

    //httppost.setEntity(str_json); 
    HttpResponse response; 

    response = httpclient.execute(httppost); 

我的manifestxml具有以下行;

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

这些是我的进口;

import java.io.IOException; 
import java.util.ArrayList; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

最有可能有事情做与你

httppost.setHeader("Content-type", "application/json"); 

application/json是不正常的内容类型HTTP POST表单提交。考虑到你没有真正发送JSON而是一个标准的POST实体(nameString =的valueString),试试这个更改为

httppost.setHeader("Content-Type", "application/x-www-form-urlencoded") 

,看看这是否将整理您的问题。或者更好的是,把它全部删除。

我在我的应用程序使用此代码 - 和它的作品完美的罚款:

HttpPost httppost = new HttpPost(SERVER_URL); 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
nameValuePairs.add(new BasicNameValuePair("REQUEST", req)); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); 
HttpResponse response = httpclient.execute(httppost); 

在这种情况下的反应是text/xml - 但是这不应该的问题。

+1

我已经评论过该行。我加了你说的话,但这没什么区别。 – 2012-04-20 15:01:19

+0

@ I.am.Will出于好奇,尝试注释掉所有'setHeader'行。只留下帖子的构造函数并设置实体 - 然后执行帖子。还要确保你的PHP代码是正确的 - 也许它确实收到请求,但你没有正确地转发它。 – 2012-04-20 15:05:59

+0

恐怕没有区别。我的php文件用于测试目的包含; '; ?> – 2012-04-20 15:12:04