Я пытаюсь отправить json
на сервер, который я написал в NodeJS
, соединение с сервером установлено. По какой-то причине я получаю FileNotFoundException
, журнал:
java.io.FileNotFoundException: http://10.0.2.2:4000/products/add
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255)
at com.example.myfstrdbcontroller.SendProductJsonTask.doInBackground(SendProductJsonTask.java:39)
at com.example.myfstrdbcontroller.SendProductJsonTask.doInBackground(SendProductJsonTask.java:21)
мой код:
public class SendProductJsonTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(strings[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData="+strings[1]);
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) {
Log.d("ccb", "doInBackground16: "+e.getCause());
Log.d("ccb", "doInBackground17: "+e.getMessage());
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("ccb", "onPostExecute: "+s);
}
}
код, который я вызываю в HTTP-запросе:
try {
productJson(nameOfProduct, serialOfProduct, priceOfProduct, getGenderRB(), uriList, getColorsCB(), getSizeCB(), new JSONMadeListener() {
@Override
public void onJSONMade(JSONObject object) {
try {
SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
ProviderInstaller.installIfNeeded(getApplicationContext());
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
new SendProductJsonTask().execute("http://10.0.2.2:4000/products/add",object.toString());
}
});
} catch (JSONException e) {
e.printStackTrace();
}