2016-09-07 2 views
-1

は、私がサーバーにデータを投稿したいが、私はこの例外を取得:ポストデータAsyncTask例外

java.net.ConnectException:localhost /を127.0.0.1:8080 - 接続 は拒否しました。

しかし、私はlocalhostの代わりにIPを使用していますが、エラーが存在します。

MainActivity

JSONObject postData = new JSONObject(); 
try { 
    postData.put("imeiNo", tLocation.getImeiNo()); 
    postData.put("latitude", tLocation.getLatitude().toString()); 
    postData.put("longitude", tLocation.getLongitude().toString()); 
    postData.put("date", tLocation.getDate()); 
    String url = "http://127.0.0.1:8080/saveTemperaryLocation"; 


    new SendDataToServer().execute(url,postData.toString()); 

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

非同期クラス、あなたのサーバーを開始していないか、接続しようとしているものclient、他の異なるportを聴いているいずれかのとき

public class SendDataToServer extends AsyncTask<String, Void, String> { 

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

    String data = ""; 

    HttpURLConnection httpURLConnection = null; 
    try { 

     httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection(); 
     httpURLConnection.setRequestMethod("POST"); 

     httpURLConnection.setDoOutput(true); 

     DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
     wr.writeBytes("saveTemperaryLocation=" + params[0]); 
     wr.flush(); 
     wr.close(); 

     InputStream in = httpURLConnection.getInputStream(); 
     InputStreamReader inputStreamReader = new InputStreamReader(in); 

     int inputStreamData = inputStreamReader.read(); 
     while (inputStreamData != -1) { 
      char current = (char) inputStreamData; 
      inputStreamData = inputStreamReader.read(); 
      data += current; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (httpURLConnection != null) { 
      httpURLConnection.disconnect(); 
     } 
    } 

    return data; 
} 

@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 
    Log.e("TAG", result); // this is expecting a response code to be sent from your server upon receiving the POST data 
} 
+3

[java.net.ConnectException:接続が拒否されました]の重複している可能性があります(http://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused) – Nikhil

答えて

0

java.net.ConnectExceptionが起こります。これがローカルなので上記のことを確認したら、JavaプログラムSocket sock=new Socket("127.0.0.1",8080)

+0

なぜ私は投稿を変更する必要があるのですか? mobでアプリケーションを実行しています – Aamir

+0

変更する必要はありません。サーバーが稼動しているエンドポイントに当たるかどうかを確認するだけです。 – Godather

関連する問題