首先,我收到状态代码200.在php脚本中,我只是将代码邮寄给我发送到服务器(我尝试调试时)。使用var_dump $ _POST或$ _REQUEST我收到一封空白电子邮件。如果我在$ _SERVER上运行foreach,我会得到一些数据,如HTTP_USER_AGENT:Apache-HttpClient / UNAVAILABLE(java 1.4)和CONTENT_LENGTH:9
我不明白为什么没有帖子数据?我是机器人新手,我实际上是在学习。
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;
答案 0 :(得分:3)
很可能与你的
有关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
- 但这不重要。