0

我正在尝试将数据从 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

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

4

2 回答 2

0

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

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

于 2013-06-09T05:59:15.963 回答
0

试试这个代码

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'];
于 2013-06-08T22:02:51.227 回答