JSON
をアンドロイドデバイスからマイサーバ(XAMPP)に送信するプログラムを作成しました。フォームを使用してPHPコードをテストし、データを正しく受信しました。未定義のインデックスで、アンドロイドがPHPコードにデータを送信しない
私のアプリは、私のサーバーにデータを送信しません。 var_dump($_POST)
を実行すると、サーバ側でarray(0)
が返されます。
ここに私のアンドロイドコードです:
public BackGround(Context context){
this.context=context;
}
@Override
protected String doInBackground(String... params){
String location_url ="http://192.168.1.90/server_connection.php";
try{
URL url1=new URL(location_url);
HttpURLConnection httpURLConnection =(HttpURLConnection)url1.openConnection();
httpURLConnection.setRequestMethod("POST");
// httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
httpURLConnection.setRequestProperty("Accept", "application/json");
OutputStream stream_upload=httpURLConnection.getOutputStream();
BufferedWriter buffer_writer=new BufferedWriter(new OutputStreamWriter(stream_upload,"UTF-8"));
String PostData= URLEncoder.encode(String.valueOf(params));
buffer_writer.write(PostData);
buffer_writer.flush();
buffer_writer.close();
stream_upload.close();
int HttpResult = httpURLConnection.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
InputStream stream_dawnload = httpURLConnection.getInputStream();
BufferedReader bufferreader = new BufferedReader(new InputStreamReader(stream_dawnload, "iso-8859-1"));
String result = "";
String line;
while ((line = bufferreader.readLine()) != null) {
result += line;
}
bufferreader.close();
stream_upload.flush();
stream_dawnload.close();
httpURLConnection.disconnect();
return result;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
私はsend_json
は私JSON
オブジェクトで、このコードを使用してデータを送信します。
backGround.execute(String.valueOf(send_json));
私は問題はJSON
オブジェクトが正しくされないことによって引き起こされていると思いますが、リクエストの本文にPOST
という文字を挿入します。
似たような問題の質問がありますが、解決策のどれも私を助けませんでした。
何か助けていただきありがとうございます。
localhostでこれをテストしていますか? –
@vikaskumarはい私はしました。このエラーはlocalHostでテストしたときに発生しました。 –
あなたのリクエストがlocalhostのURLに当てられていますか?同じネットワーク上にいて、モバイルからのリクエストがあなたのマシンのローカルホストを経由するようにしてください。 –