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
誰かが私を助けてくれたらとてもうれしいです!