我正在尝试将一些json字符串发布到rest服务器,但是我在获取输入流时遇到了一个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)
可能导致此问题的原因以及如何解决?
答案 0 :(得分:8)
如果是4 **错误HTTP代码使用
connection.getErrorStream();
而不是
connection.getInputStream();
connection是HttpURLConnection的类型。
答案 1 :(得分:2)
URL连接上的FileNotFound意味着目标文档不可用,即提供的URL错误,或者服务器返回4 *代码。检查目标资源是否可用
答案 2 :(得分:1)
尝试将此设置:connection.setDoOutput(false);
设置为您的连接 - 希望它有所帮助;)
答案 3 :(得分:0)
在将数据写入流之前添加“connection.connect()”行。
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(formsubmissionJson);
writer.flush();
答案 4 :(得分:0)
我不知道为什么,但在添加以下代码行后,问题就解决了。
connection.setInstanceFollowRedirects(false);
答案 5 :(得分:0)
如果您在服务器端使用GET
方法,则会出现此错误。
使其有效:
将connection.setRequestMethod("POST");
更改为connection.setRequestMethod("GET");
并删除以下代码。
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(formsubmissionJson);
writer.flush();
或者只是简单地将GET
方法更改为服务器上的POST
而不进行上述更改。