Я хочу отправить данные на сервер, но я получаю это исключение:Сообщения AsyncTask данных Исключение
java.net.ConnectException: локальный/127.0.0.1: 8080 - Подключение отказалось.
Однако я использую IP вместо localhost, но ошибка существует.
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();
}
Асинхронный класс
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
}
Возможный дубликат [java.net.ConnectException: Connection failed] (http://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused) – Nikhil