Я пытаюсь отправить файл JSON на мой Python сервер. Почтальон делает это нормально, но в Android Studio у меня проблема, сервер говорит мне, что в полученных данных нет содержимого. Вот мой исходный код:
Основной класс:
String ipAddress="http://xx.xx.xxx.xxx:8080";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("test", "test");
jsonObject.put("test", "test");
jsonObject.put("test", "test");
jsonObject.put("test", "test");
new InteractWithServer().execute(ipAddress,jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Класс InteractWithServer:
public class InteractWithServer extends AsyncTask<String, Void,String> {
@Override
protected String doInBackground(String... strings) {
String result = "";
HttpURLConnection httpURLConnection=null;
try {
httpURLConnection = (HttpURLConnection) new URL(strings[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setDoInput(true);
// I have tried this and it also doesn't work
// OutputStream output = httpURLConnection.getOutputStream();
// output.write(strings[1].getBytes("UTF-8"));
// InputStream response=httpURLConnection.getInputStream();
// This too
// DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
// wr.writeBytes(strings[1]);
// wr.flush();
// wr.close();
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(strings[1]);
writer.close();
os.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
result += current;
}
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("RESULT: ",s);
}
}
Вот что говорит мне, что предположительно неправильно:
W/System.err: java.io.FileNotFoundException: http://xx.xx.xxx.xxx:8080
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
W/System.err: at ro.ase.musapp.InteractWithServer.doInBackground(InteractWithServer.java:55)
at ro.ase.musapp.InteractWithServer.doInBackground(InteractWithServer.java:21)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)