http://www.itstrike.cn/Question/29e46085-7fd4-4deb-afd7-4572ed5d591a.html
https://stackoverflow.com/questions/20987525/filenotfoundexception-trying-to-send-json-to-net-webservice-using-httpurlconnec
I am trying to parse a response from an
Here is the
EDIT:The operation as specified in WSDL:
Adding EDIT:Using the
|
shareimprove this question |
asked Jan 8 ‘14 at 5:14 1,87873692 |
|
You‘ve omitted the actual exception. – EJP Jan 8 ‘14 at 5:16 |
||
|
@EJP added the exception and the URL – vamsiampolu Jan 8 ‘14 at 5:26 |
||
|
Now that you‘ve provided it, what part of not found ... http://888topup.com/ImageProcess.svc/UploadImage don‘t you understand? – EJP Jan 8 ‘14 at 5:27
|
||
|
I tried passing JSON data to output stream to the service,does it mean that the request was not successfull,how do I get the response from the service without using InputStream – vamsiampolu Jan 8 ‘14 at 5:30 |
||
|
You‘re missing the point. It‘s the URL itself that‘s invalid. Nothing to do with the code. – EJP Jan 8 ‘14 at 5:31 |
1 Answer
up vote0down voteaccepted |
There were a few mistakes with what I was doing,firstly I was passing the wrong parameter
Secondly,I was trying to parse a response when no response was being sent.So, I replace the code to get the
The entire code is here:
And finally I used this to send the data to the web-service as binary data:
|
||||
|
我想解析从响应 InputStream
从 HttpURLConnection
使用以下代码。试图获取时出现错误 InputStream
。我只想要得到的响应来检查是否 web servcice 调用工作 OK
。
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 工程从传递煎茶触摸别人的人对其进行测试。
尝试将 JSONObject 作为字符串传递而不字节的数据。做了也不起作用吗?
解决方法 1:
有几个错误是在做什么,首先我传递错误的参数 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"); }
第二,我想解析响应时不正在发送响应。所以,我替换代码来获取 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());