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

このコードを試してください

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 に答える