2016-09-07 3 views
0

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という文字を挿入します。

似たような問題の質問がありますが、解決策のどれも私を助けませんでした。

何か助けていただきありがとうございます。

+0

localhostでこれをテストしていますか? –

+0

@vikaskumarはい私はしました。このエラーはlocalHostでテストしたときに発生しました。 –

+0

あなたのリクエストがlocalhostのURLに当てられていますか?同じネットワーク上にいて、モバイルからのリクエストがあなたのマシンのローカルホストを経由するようにしてください。 –

答えて

0

this question's solutionを読んだ後、私は私のコードを編集した: は最初、私は私のサービスからbackGround.execute(String.valueOf(send_json));を排除し、私は以下のように、asyncTaskにパラメータとしてvriablesを送っ:

backGround.execute(String.valueOf(mylocation.getLatitude()),String.valueOf(mylocation.getLongitude())); 

が、その後asyncTaskクラスで私はJSONオブジェクトを作ります。 これは私のdoInbackgroundのコードです:

@Override 
protected String doInBackground(String... params){ 


    String location_url ="http://192.168.1.90/connection.php"; 


try{ 
     URL url1=new URL(location_url); 

     HttpURLConnection httpURLConnection =(HttpURLConnection)url1.openConnection(); 

     httpURLConnection.setRequestMethod("POST"); 
     httpURLConnection.setDoOutput(true); 
     httpURLConnection.setDoInput(true); 
     httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
     httpURLConnection.setRequestProperty("Accept", "application/json"); 
     httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); 



     OutputStream stream_upload=httpURLConnection.getOutputStream(); 
     BufferedWriter buffer_writer=new BufferedWriter(new OutputStreamWriter(stream_upload,"UTF-8")); 

    JSONObject obj = new JSONObject(); 

    try { 
     obj.put("Latitude", params[0]); 
     obj.put("Longitude", params[1]); 



    }catch (JSONException e){ 
     e.printStackTrace(); 
    } 

    httpURLConnection.connect(); 

    buffer_writer.write(obj.toString()); 
    buffer_writer.flush(); 
    buffer_writer.close(); 
    stream_upload.close(); 





     int status = httpURLConnection.getResponseCode(); 
     if(status ==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; 

} 

、最も重要な注意事項があることである:uは、サーバーにJSONオブジェクトを送信する場合、uはこのコードを追加する必要があります

$json = file_get_contents('php://input'); 

echo (file_get_contents('php://input'));//for being aware of what is in the object. 
$request = json_decode($json, true); 

とJSONの値を使用するための:

$location_lat=$request['Latitude']; 
$location_long=$request['Longitude']; 

そして他の一つは、私が$location_lat=$_POST['Latitude']をしようと試み、そして再び私が求めていたこのエラーは、apearedさ:不定Index.So私はJSON O使用のためになりましたbjectsこのメソッドは正しくありません。 希望すると便利です。

0

ここで注意すべき点がいくつかあります。

最初に言及したように、あなたのJSONデータがあなたの要求に適切にフォーマットされていないことが主な原因です。 を指定したAsyncTaskのようにデータを渡しています。execute(send_json)を呼び出すときに渡したオブジェクトStringString[]オブジェクトに変換されます。 String.valueOf(params)を呼び出すと、最初にAsyncTaskに渡したデータに似ているものは返されませんが、データが含まれているString配列のテキストが返されます。

第2に、サードパーティのライブラリを使用してネットワークコードを処理することを強くお勧めします。 HTTP接続を適切に管理するために記述しなければならないコードは、簡単でも簡潔でもありません。そこにははるかに優れた選択肢があります。 OkHttpは素晴らしい例です。私はあなたにそれを使用することを強くお勧めします。

ところで、私はあなたがjava/androidのHTTP接続をどのように作成して管理するかを知ることをお勧めしません。あなたは間違いなくあなたがデータを送受信するときに何が起こっているのかを理解しようとするべきです。私はちょうどあなたが非常に良い仕事を正確に行うライブラリがあるので、あなたはすべての複雑さを自分で処理する必要はないと言っています。

+0

ありがとう、私は答えを追加、あなたが好きなら、あなたはそれを確認することができます。 –

関連する問題