7

我正在尝试将一些 json 字符串发布到休息服务器,但在获取输入流时我得到一个 java 文件未找到异常。

这是我的代码:

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();
                   }


                  }



        }

我得到的系统错误如下:

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)

什么可能导致这种情况,我该如何解决?

4

6 回答 6

10

如果出现 4** 错误 HTTP 代码,请使用

connection.getErrorStream();

代替

connection.getInputStream();

连接是 HttpURLConnection 的类型。

于 2014-10-08T12:58:09.843 回答
3

URL 连接上的 FileNotFound 意味着目标文档不可用,即提供的 URL 错误,或者服务器返回 4* 代码。检查目标资源是否可用

于 2013-08-21T09:59:00.457 回答
1

尝试设置这个:connection.setDoOutput(false);到你的连接 - 希望它有帮助;)

于 2014-05-18T10:08:54.407 回答
0

在将数据写入流之前添加“connection.connect()”行。

connection.connect();

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
                    writer.write(formsubmissionJson);                   
writer.flush();
于 2015-06-04T14:29:31.643 回答
0

我不知道为什么,但是在添加以下代码行后问题就解决了。

connection.setInstanceFollowRedirects(false);
于 2015-07-08T06:15:14.150 回答
0

如果您GET在服务器端使用方法,则会出现此错误。

为了使它工作:

更改connection.setRequestMethod("POST");connection.setRequestMethod("GET");删除以下代码。

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

或者只是简单地将GET方法更改为POST在服务器上而不进行上述更改。

于 2017-05-27T11:10:35.787 回答