Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Http url connection Throwing File Not Found Exception at getInputstream

I am trying to post some json string to a rest sever but I'm getting a java file not found exception at get input stream.

Here is my code:

package com.muzima.view.sample.activities;

import javax.servlet.http.HttpServletResponse;

import com.muzima.view.sample.R;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class SyncFormDataActivity extends Activity implements OnClickListener{

        private EditText value;

        private Button btn;

        private static final String METHOD_POST = "POST";

        private static final String URL = "/ws/rest/v1/muzima/queueData";



    @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_sync_form_data);

                String formsubmissionJson = getIntent().getStringExtra("formdata");
            String URF = (getString(R.string.default_server) + URL);

            System.out.println("url is" +URF);

                /* Testing to see if  we can get the json string from form in webview*/

                System.out.println("submit 56565 ======= " + formsubmissionJson);  

                btn=(Button)findViewById(R.id.button1);

                btn.setOnClickListener(this);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.layout.menu, menu);
                return true;

        }

        @Override
        protected void onDestroy() {
           super.onDestroy();

        }

        @Override
        protected void onResume() {
            super.onResume();

        }

        @Override
        protected void onPause() {
           super.onPause();
            }

        public void onClick(View v) {
        // TODO Auto-generated method stub
                String formsubmissionJson = getIntent().getStringExtra("formdata");
                new MyAsyncTask().execute(formsubmissionJson.toString());      

        }

        private class MyAsyncTask extends AsyncTask<String, String, String>{

        @Override
        protected void onPreExecute() {
           super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... args) {
                try {
                                postingQueueData();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();

                        }
                // TODO Auto-generated method stub
                        return null;
        }

        protected void onPostExecute(Double result){

        Toast.makeText(getApplicationContext(), "Form Data has been sent to server", Toast.LENGTH_LONG).show();

                // Start ListPatient activity
    Intent ip = new Intent(getApplicationContext(), ListPatientActivity.class);
       startActivity(ip);

        }


        public void postingQueueData() throws Exception   {
                    String formsubmissionJson = getIntent().getStringExtra("formdata");
                    URL url = new URL("http://192.168.1.3:8081/openmrs-standalone/ws/rest/v1/muzima/queueData");

                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    String encodedAuthorization = "Basic " + Base64.encodeToString("admin:test".getBytes(), Base64.NO_WRAP);
                   // String encodedAuthorization = "Basic " + Base64.encodeToString("admin:test".getBytes(), Base64.NO_WRAP);
                    connection.setRequestProperty("Authorization", encodedAuthorization);
                    connection.setRequestMethod(METHOD_POST);
                    connection.setRequestProperty("Content-Type", "application/json");
                    connection.setDoOutput(true);

                    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
                    writer.write(formsubmissionJson);
                    writer.flush();

                    InputStreamReader reader = new InputStreamReader(connection.getInputStream(),"UTF-8");
                    int responseCode = reader.read();
                    if (responseCode == HttpServletResponse.SC_OK
                            || responseCode == HttpServletResponse.SC_CREATED) {
                        //log.info("Queue data created!");
                    }

                    reader.close();
                    writer.close();
                   }


                  }



        }

The system error I'm getting is as follows:

08-21 10:49:26.389: W/System.err(8640): java.io.FileNotFoundException: http://192.168.1.3:8081/openmrs-standalone/ws/rest/v1/muzima/queueData
08-21 10:49:26.399: W/System.err(8640):         at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
08-21 10:49:26.399: W/System.err(8640):         at com.muzima.view.sample.activities.SyncFormDataActivity$MyAsyncTask.postingQueueData(SyncFormDataActivity.java:135)
08-21 10:49:26.399: W/System.err(8640):         at com.muzima.view.sample.activities.SyncFormDataActivity$MyAsyncTask.doInBackground(SyncFormDataActivity.java:98)
08-21 10:49:26.399: W/System.err(8640):         at com.muzima.view.sample.activities.SyncFormDataActivity$MyAsyncTask.doInBackground(SyncFormDataActivity.java:1)
08-21 10:49:26.399: W/System.err(8640):         at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-21 10:49:26.399: W/System.err(8640):         at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-21 10:49:26.399: W/System.err(8640):         at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-21 10:49:26.399: W/System.err(8640):         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-21 10:49:26.409: W/System.err(8640):         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-21 10:49:26.409: W/System.err(8640):         at java.lang.Thread.run(Thread.java:1019)

What might cause this and how do I solve it?

like image 689
user2499766 Avatar asked Aug 21 '13 09:08

user2499766


1 Answers

In case of 4** Error HTTP Codes use

connection.getErrorStream();

Instead of

connection.getInputStream();

connection is type of HttpURLConnection.

like image 138
Ayman Mahgoub Avatar answered Nov 14 '22 22:11

Ayman Mahgoub