码迷,mamicode.com
首页 > Web开发 > 详细

zz 试图将 JSON 发送到.NET web 服务使用 HttpURLConnection FileNotFoundException

时间:2017-07-25 13:37:41      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:$0   adl   gzip   html   method   creat   wrong   taglist   解决   

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 InputStream from a HttpURLConnection using the following code.I get an error when trying to get the InputStream.I am just looking to get a response to check if the web-servcice call worked 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);
    }

Here is the Logcat entry for what happened when I tried to run this code:

  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)

EDIT:

The operation as specified in 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>

Adding urlConnection.connect() before writing data to the OutputStream does not work either.

EDIT:Using the URL(SOAP_ACTION) and passing jsondata works from Sencha-Touch for someone else who tested it.

Tried passing JSONObject as String instead of byte data.Did not work either?

shareimprove this question
 
1  
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

There were a few mistakes with what I was doing,firstly I was passing the wrong parameter Vaueinstead of Value to the service.

   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");
    }

Secondly,I was trying to parse a response when no response was being sent.So, I replace the code to get the InputStream with the following code:

 int response=urlConnection.getResponseCode();

The entire code is here:

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);
    } 

And finally I used this to send the data to the web-service as binary data:

  DataOutputStream dos=new DataOutputStream(urlConnection.getOutputStream());
    dos.write(obj.toString().getBytes());
shareimprove this answer
 
    
You could avoid many problems when not using HttpURLConnection directly, but use a tiny wrapper likeDavidWebbHere is a Gist where you can see an example. – hgoebl Jan 8 ‘14 at 7:52

我想解析从响应 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());

zz 试图将 JSON 发送到.NET web 服务使用 HttpURLConnection FileNotFoundException

标签:$0   adl   gzip   html   method   creat   wrong   taglist   解决   

原文地址:http://www.cnblogs.com/liangouyang/p/7233442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!