我正在尝试将一些 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)
什么可能导致这种情况,我该如何解决?