#StackBounty: #java #android Why am I getting a java.io.FileNotFoundException: http://10.0.2.2:4000/products/add when trying to send ht…

Bounty: 50

Im trying to send a json to a server I wrote in NodeJS, a connection is made to the server,
For some reason I get a FileNotFoundException,
the log:

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)

my code:

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);
}

}

the code I do call the http request:

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();
            }


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.