【问题标题】:PHP $_POST empty when passing data through Android通过Android传递数据时PHP $_POST为空
【发布时间】:2013-06-04 22:25:16
【问题描述】:

我正在尝试将数据从 Android 传递到我的 PHP 应用程序,但是一旦所有 $_POST 变量为空,POST 方法似乎无法正常工作。

我的 Android 活动的一部分:

String email = inputEmail.getText().toString();
String name = inputName.getText().toString();
String password = inputPassword.getText().toString();
String date  = inputDate.getText().toString();

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("date", date));

// Getting JSON Object
JSONObject json = jsonParser.makeHttpRequest(url, "POST", params);

现在,在我的 JSONParser.java 上:

 // check for request method
 if(method == "POST"){
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params));

    httpPost.addHeader("content-type", "application/json");

    HttpResponse httpResponse = httpClient.execute(httpPost);
    Log.v("response code", httpResponse.getStatusLine().getStatusCode() + ""); 
    HttpEntity httpEntity = httpResponse.getEntity();
    is = httpEntity.getContent();

我省略了代码的不相关部分,因为我实际上到达了服务器,只得到了空的 $_POST 变量。

奇怪的是,如果我删除这行...

httpPost.addHeader("content-type", "application/json");

...请求根本不起作用。

我正在关注本教程:AndroidHive

如果有人可以帮助我,我会很高兴!

【问题讨论】:

  • all $_POST variables are empty,当请求是application/x-www-form-urlencoded而不是application/json时填充$_POST变量
  • 那么我该如何解决这个问题?如果我更改为: httpPost.addHeader("content-type", "application/x-www-form-urlencoded") 我会收到著名的错误:“解析数据时出错 org.json.JSONException: End of input at character 0” .提前致谢。
  • 但是你还是会发送 json 对吧?
  • 您可以发布通话的 JSON 响应吗?也许它以某种方式坏了
  • @Musa,如果我把 httpPost.addHeader("content-type", "application/json") 和 httpPost.addHeader("content-type", "application/x-www-form -urlencoded") 我得到了同样的错误(没有 $_POST)。

标签: php android json


【解决方案1】:

试试这个代码

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("date", date));
// Getting JSON Object
JSONObject json = JSONfunctions.getJSONfromURL(url, nameValuePairs);

JSONfunctions

public static JSONObject getJSONfromURL(String url, ArrayList<NameValuePair> nameValuePairs) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    InputStream is = getData(url, nameValuePairs);
    String result = "";
    JSONObject jArray = null;
    // convert response to string
    try {
        new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        InputStreamReader r = new InputStreamReader(is, "UTF-8");
        int intch;
        while ((intch = r.read()) != -1) {
            char ch = (char) intch;
            // Log.i("app", Character.toString(ch));
            String s = new String(Character.toString(ch).getBytes(), "UTF-8");
            sb.append(s);
        }
        is.close();
        result = sb.toString();
        Log.i("JSON", result);
        jArray = new JSONObject(result);

    } catch (JSONException e) {
        Log.e(TAG, "Error parsing data " + e.toString());

    } catch (Exception e) {
        Log.e(TAG, "Error converting result " + e.toString());

    }
    return jArray;
}


public static InputStream getData(String Url, ArrayList<NameValuePair> nameValuePairs) {
    InputStream is = null;

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Url);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (ClientProtocolException e) {
        Log.e(TAG, "Error in http connection " + e.getMessage().toString());

    } catch (IOException e) {
        Log.e(TAG, "Error in http connection " + e.getMessage().toString());

    } catch (Exception e) {
        Log.e(TAG, "Error in http connection " + e.getMessage().toString());

    }
    return is;

}

你的 PHP 代码

 $name = $_POST['name'];
 $email = $_POST['email'];
 $password = $_POST['password'];
 $date = $_POST['date'];

【讨论】:

  • 不得不做一些调整,例如paramsList&lt;NameValuePair&gt;而不是ArrayList&lt;NameValuePair&gt;。尽管如此,再次得到Error parsing data org.json.JSONException: End of input at character 0。感谢您的帮助。
  • 实际上没有httpPost.addHeader("content-type", "application/json")我无法访问服务器。使用您的功能时,它似乎也达不到。您可以在rafael.daniels1006.webfactional.com/users/addUser 中查看我的 JSON。显然每个$_POST 参数都是空的,因为没有设置任何内容,但是通过Android访问时不应该发生这种情况。
【解决方案2】:

终于解决了这个问题。 PhpMyAdmin 配置不正确,然后我试图解析不是 JSON 对象的东西。

现在不需要httpPost.addHeader("content-type", "application/json");,正如@Musa 巧妙地观察到的那样。谢谢大家的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-09
    • 2017-12-03
    • 2012-12-13
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多