FileNotFoundException尝试使用HttpURLConnection将JSON发送到.NET webservice

时间:2014-01-08 05:14:06

标签: java android inputstream httpurlconnection

我正在尝试使用以下代码解析来自InputStream的{​​{1}}的响应。我在尝试获取HttpURLConnection时出错。我只是想获取检查web-servcice调用是否有效的响应InputStream

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

编辑:

WSDL中指定的操作:

  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: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并将来自Sencha-Touch的jsondata传递给其他测试它的人。

尝试将JSONObject作为String而不是字节数据传递.Did也不起作用?

1 个答案:

答案 0 :(得分: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");
    }

其次,当我没有发送任何响应时,我试图解析响应。所以,我替换代码以使用以下代码获取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());