6

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?

6
  • Need your relevant code Commented Aug 21, 2013 at 9:56
  • So where is that code exactly ?? :> Commented Aug 21, 2013 at 9:57
  • @user2499766-Just show your code what u have tried with logcat error Commented Aug 21, 2013 at 9:58
  • code> pastebin.com/pdkaxxuQ – user2499766 Commented Aug 21, 2013 at 10:13
  • 1
    Just edit your code and error messages into your post the next time. Commented Aug 21, 2013 at 10:23

6 Answers 6

9

In case of 4** Error HTTP Codes use

connection.getErrorStream();

Instead of

connection.getInputStream();

connection is type of HttpURLConnection.

Sign up to request clarification or add additional context in comments.

Comments

3

FileNotFound on URL connection means that target document is not available aka URL provided is wrong, or server returns 4* code. Check if target resource is available

5 Comments

i am sending json string to a rest url. Using advanced rest chrome plugin i am able to send the same json string
If you are using inputStream than you want to RECEIVE something not SEND
what do you mean by than you want to RECEIVE something not SEND
Can you open this url in your browser? http://192.168.1.3:8081/openmrs-standalone/ws/rest/v1/muzima/queueData
yes i can access the url the following is what i get when i access it directly from the browser pastebin.com/yGre0jAvhttp://pastebin.com/yGre0jAv
1

Try setting this: connection.setDoOutput(false); to your connection - hope it helps ;)

1 Comment

what's that mean?
0

add "connection.connect()" line before writing data to the stream.

connection.connect();

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

Comments

0

I dont know why, but after adding the following line of code the problem was solved.

connection.setInstanceFollowRedirects(false);

Comments

0

If you used GET method on server side then it will give this error.

To make it works:

Change connection.setRequestMethod("POST"); to connection.setRequestMethod("GET"); and remove this following code.

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

Or just simply change GET method to POST on server without doing changes above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.