0

My code to that sends JSON over the network works until OutputStream is invoked, but when I try actually sending the data, I get the following exception. Can anyone help me figure it out?

java.io.FileNotFoundException: http://hmkcode.appspot.com/jsonservlet at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238) at app.test.testapp2.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:84)

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            button = (Button) findViewById(R.id.accept);
            textView = (TextView) findViewById(R.id.textView);
            editName = (EditText) findViewById(R.id.editName);
            editCountry = (EditText) findViewById(R.id.editCountry);
            editTwitter = (EditText) findViewById(R.id.editTwitter);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    HttpAsyncTask httpAsyncTask = new HttpAsyncTask(MainActivity.this);
                    httpAsyncTask.execute("http://hmkcode.appspot.com/jsonservlet", editName.getText().toString(), editCountry.getText().toString(), editTwitter.getText().toString());
                }
            });
        }

        private class HttpAsyncTask extends AsyncTask<String, Void, String> {
            private MainActivity mainActivity;
            String result = "";

            HttpAsyncTask(MainActivity mainActivity) {
                this.mainActivity = mainActivity;
            }

            @Override
            protected String doInBackground(String... strings) {
                Person person = new Person();
                person.setName(strings[1]);
                person.setCountry(strings[2]);
                person.setTwitter(strings[3]);

                try {
                    URL url = new URL(strings[0]);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();


                    String json = "";

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.accumulate("name", "!!!");
                    jsonObject.accumulate("country", "@@@");
                    jsonObject.accumulate("twitter", "####");

                    json = jsonObject.toString();

                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    httpURLConnection.setRequestProperty("Accept", "application/json");
                    httpURLConnection.setRequestProperty("content-type", "application/json");
                    InputStream is = httpURLConnection.getInputStream();
                    OutputStream os = httpURLConnection.getOutputStream();
                    os.write(json.getBytes("utf-8"));
                    os.flush();

                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
                    String line = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        result += line;
                    }
                    is.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result) {
                str = result;
                mainActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONArray jsonArray = new JSONArray(str);
                            mainActivity.textView.setText(jsonArray.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }


W/System.err: java.io.FileNotFoundException: http://hmkcode.appspot.com/jsonservlet
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
W/System.err:     at app.test.testapp2.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:84)
.....
Suresh A
  • 1,178
  • 2
  • 10
  • 21

1 Answers1

0

This is equivalent to HTTP status 404 - Not Found.

user207421
  • 305,947
  • 44
  • 307
  • 483