1

i am trying to send a JSONObject as input parameter for a C# Web API with the following code

protected String doInBackground(JSONObject... companyInfo) {

    try {
        URL url;
        URLConnection urlConn;

        url = new URL(HOST_NAME + WEB_API_METHOD);

        urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/json");
        urlConn.setRequestProperty("Host", HOST_NAME);
        urlConn.connect();

        PrintWriter out = new PrintWriter(urlConn.getOutputStream());
        out.print(companyInfo[0]);
        out.close();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
        bufferedReader.close();

        Log.i("companyInfo > ", stringBuilder.toString());
    }
    catch (Exception ex)
    {
        Log.e("Exception", ex.getLocalizedMessage());
    }
    return null;
}

When this is called, the method doesn't execute and when i try to catch the error, i don't get a meaningful message. it shows the URL name and thats it.

I am unable to find the issue and any help would be appreciated.

this is the error i got

java.io.FileNotFoundException: http://MY_FULL_URL
     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
     at com.fourtyninetons.NewCompanyActivity$saveCompanyProfile.doInBackground(NewCompanyActivity.java:103)
     at com.fourtyninetons.NewCompanyActivity$saveCompanyProfile.doInBackground(NewCompanyActivity.java:70)
     at android.os.AsyncTask$2.call(AsyncTask.java:295)
     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
     at java.lang.Thread.run(Thread.java:818)

Lakshman.

Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48

1 Answers1

0

As mentioned in the solution of this question, the problem is with the web api method and the backend database. Fiddler helped me solve the issue and the above code is working fine.

Useful Tip: When anyone gets this FileNotFoundExceptionuser POSTMAN or FIDDLER to execute the method and find for the errors.

Thank you everyone who attempted to solve this.

Community
  • 1
  • 1
Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48