Search code examples
javaphpandroidmysqlandroid-asynctask

My InputStream is returning java.IO.FileNotFoundException with my website after establishing a POST request to save on mySQL


My InputStream is returning java.IO.FileNotFoundException with my website address after establishing a POST request to save on MySQL. I was successful once writing to MySQL server but it just stopped working. I am trying to insert data to MySQL on my server. The PHP files for opening a connection and inserting are all correct but doing it in the background async is returning IOException.

Here is my code

String users_url = "https://gdihq.com/evaluate/register.php";
        String method = params[0];
        if(method.equals("Users")){
            String Date = params[1];
            String FirstName = params[2];
            String LastName = params[3];
            String Phone = params[4];
            String Email = params[5];
            try {
                URL url = new URL(users_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
                String data = URLEncoder.encode("mDate","UTF-8") + "=" + URLEncoder.encode(Date,"UTF-8") + "&" +
                        URLEncoder.encode("FirstName","UTF-8") + "=" + URLEncoder.encode(FirstName,"UTF-8") + "&" +
                        URLEncoder.encode("LastName","UTF-8") + "=" + URLEncoder.encode(LastName,"UTF-8") + "&" +
                        URLEncoder.encode("Phone","UTF-8") + "=" + URLEncoder.encode(Phone,"UTF-8") + "&" +
                        URLEncoder.encode("Email","UTF-8") + "=" + URLEncoder.encode(Email,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS,"iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null){
                    result += line;
                }
                bufferedReader.close();
                IS.close();

                httpURLConnection.disconnect();
                return result;


            } catch (MalformedURLException e) {
                Log.d(TAG,"Stacktrace Malfunctioned Begins here");
                Show(String.valueOf(e.getStackTrace()),ctx);
            } catch (IOException e) {
                Log.d(TAG,"Stacktrace Begins here");
                e.printStackTrace();

            }
        }
        return null;

Here under is the IOException is throws

java.io.FileNotFoundException: https://gdihq.com/evaluate/register.php
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:259)
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at com.example.gdiapp.BackgroundTask.doInBackground(BackgroundTask.java:63)
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at com.example.gdiapp.BackgroundTask.doInBackground(BackgroundTask.java:25)
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at android.os.AsyncTask$3.call(AsyncTask.java:378)
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-01-09 12:55:08.596 12651-31948/com.example.gdiapp W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
2021-01-09 12:55:08.597 12651-31948/com.example.gdiapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-01-09 12:55:08.597 12651-31948/com.example.gdiapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-01-09 12:55:08.597 12651-31948/com.example.gdiapp W/System.err:     at java.lang.Thread.run(Thread.java:919)

But the website is correct. What is my error?


Solution

  • After scouting the internet and tried many options, I understood the responseCode 409 means there is a conflict and this conflict does not necessarily happen all the time. It is conflict with NAME. I changed my PHP file https://gdihq.com/evaluate/register.php to https://gdihq.com/evaluate/registerAccount.php and the problem was gone.