【问题标题】:Android http post not sending dataAndroid http post不发送数据
【发布时间】:2012-04-20 14:36:38
【问题描述】:

首先,我收到状态代码 200。在 php 脚本中,我只是让代码将发送到服务器的内容发送给我(同时我尝试调试)。使用 var_dump $_POST 或 $_REQUEST 我收到一封空白电子邮件。如果我在 $_SERVER 上运行 foreach,我会返回一些数据,例如 HTTP_USER_AGENT :Apache-HttpClient/UNAVAILABLE (java 1.4) 和 CONTENT_LENGTH : 9

我不明白为什么没有帖子数据?我是 android 新手,我正在边做边学。

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;

【问题讨论】:

    标签: android json post web http-post


    【解决方案1】:

    这很可能与你有关

    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 - 但这不重要。

    【讨论】:

    • 我已经注释掉了那行。我添加了您所说的内容,但没有任何区别。
    • @I.am.Will 出于好奇,尝试注释掉所有 setHeader 行。只留下帖子的构造函数并设置实体 - 然后执行帖子。还要确保你的 PHP 代码是正确的——也许它确实收到了请求,但你没有正确转发它。
    • 恐怕没什么区别。我用于测试目的的 php 文件包含; '); ?>
    • @I.am.Will 你的问题是:var_dump 是一个 void 函数 - 它不返回任何内容,因此将其返回值分配给 $string 将使该字符串为空。改为使用:$string = print_r($_REQUEST, true);
    • 将标题设置为application/x-www-form-urlencoded 对我有用。
    猜你喜欢
    • 2013-05-12
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多