我試圖解析從HttpURLConnection
使用以下代碼從InputStream
的響應。我試圖獲得InputStream
時出現錯誤。我只是希望得到一個迴應檢查web-servcice電話是否工作OK
。FileNotFoundException嘗試使用HttpURLConnection發送JSON到.NET webservice
static final String SOAP_ACTION="http://888topup.com/ImageProcess.svc/UploadImage";
java.net.URL url=null;
try {
url = new java.net.URL(SOAP_ACTION);
} catch (MalformedURLException e) {
Log.e(TAG, "Bad URL",e);
}
JSONObject obj=new JSONObject();
try {
obj.put("Vaue", value);
} catch (JSONException e) {
Log.e(TAG, "Create JSONObjerct throws an error");
}
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection)url.openConnection();
} catch (IOException e1) {
Log.e(TAG,"Error opening connection",e1);
}
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
try {
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.addRequestProperty("Accept-Encoding", "gzip deflate sdch");
urlConnection.setRequestProperty("Content-Type","application/json");
} catch (ProtocolException e) {
Log.e(TAG,"Error setting header",e);
}
try {
OutputStream os=urlConnection.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(os);
byte[] data=obj.toString().getBytes();
bos.write(data);
InputStream is=urlConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String response;
response=br.readLine();
Log.d(TAG, "Response: "+response);
bos.close();
br.close();
is.close();
urlConnection.disconnect();
} catch (IOException e) {
Log.e(TAG,"Error peforming read-write operations",e);
}
這裏是當我試圖運行此代碼發生了什麼事Logcat
項:
MainActivity(9628): Error peforming read-write operations
MainActivity(9628): java.io.FileNotFoundException: http://888topup.com/ImageProcess.svc/UploadImage
MainActivity(9628): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
MainActivity(8989): at com.example.imageuploadtest.MainActivity.sendStringToServiceUsingRest(MainActivity.java:184)
MainActivity(8989): at com.example.imageuploadtest.MainActivity.access$0(MainActivity.java:149)
MainActivity(8989): at com.example.imageuploadtest.MainActivity$1$1.run(MainActivity.java:66)
編輯:
在WSDL指定的操作:
<wsdl:operation name="UploadImage">
<wsdl:input wsaw:Action="http://tempuri.org/IImageProcess/UploadImage" message="tns:IImageProcess_UploadImage_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IImageProcess/UploadImageResponse" message="tns:IImageProcess_UploadImage_OutputMessage"/>
</wsdl:operation>
在寫入數據之前添加urlConnection.connect()
OutputStream
也不起作用。
編輯:使用URL(SOAP_ACTION)
並將jsondata從Sencha-Touch傳遞給其他測試它的人。
嘗試傳遞JSONObject作爲字符串而不是字節data.Did不工作嗎?
你已經省略了實際的例外。 – EJP
@EJP增加了例外和URL – vamsiampolu
現在你已經提供了它,'你沒有找到...... http:// 888topup.com/ImageProcess.svc/UploadImage'什麼部分你不明白嗎? – EJP