通过Android传递数据时,PHP $ _POST为空

时间:2013-06-08 21:23:45

标签: php android json

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

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

2 个答案:

答案 0 :(得分: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'];

答案 1 :(得分:0)

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

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