2014-01-08 26 views
0

我試圖解析從HttpURLConnection使用以下代碼從InputStream的響應。我試圖獲得InputStream時出現錯誤。我只是希望得到一個迴應檢查web-servcice電話是否工作OKFileNotFoundException嘗試使用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不工作嗎?

+1

你已經省略了實際的例外。 – EJP

+0

@EJP增加了例外和URL – vamsiampolu

+0

現在你已經提供了它,'你沒有找到...... http:// 888topup.com/ImageProcess.svc/UploadImage'什麼部分你不明白嗎? – EJP

回答

0

我在做什麼時出現了一些錯誤,首先我將錯誤的參數Vaue而不是Value傳遞給服務。

JSONObject obj=new JSONObject(); 
    try { 
     String value_key="Value"; 
     obj.put(value_key, value); 
     Log.d(TAG,obj.toString()); 
    } catch (JSONException e) { 
     Log.e(TAG, "Create JSONObjerct throws an error"); 
    } 

其次,我試圖分析時,沒有響應,是sent.So的響應,我的代碼替換爲下面的代碼來獲取InputStream

int response=urlConnection.getResponseCode(); 

整個代碼是在這裏:

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 { 
     String value_key="Value"; 
     obj.put(value_key, value); 
     Log.d(TAG,obj.toString()); 
    } 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); 
    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 { 
     urlConnection.connect(); 
     DataOutputStream dos=new DataOutputStream(urlConnection.getOutputStream()); 
     dos.write(obj.toString().getBytes()); 
     int response=urlConnection.getResponseCode(); 
     Log.d(TAG, "Response code: "+response); 
     urlConnection.disconnect(); 
    } catch (IOException e) { 
     Log.e(TAG,"Error peforming read-write operations",e); 
    } 

最後,我用這個數據發送到Web服務作爲二進制數據:

DataOutputStream dos=new DataOutputStream(urlConnection.getOutputStream()); 
    dos.write(obj.toString().getBytes()); 
+0

當不直接使用'HttpURLConnection'時,可以避免很多問題,但使用像[DavidWebb](https://github.com/hgoebl/DavidWebb)這樣的小包裝。 [這是一個Gist](https://gist.github.com/hgoebl/8313248)你可以看到一個例子。 – hgoebl

相關問題