java - 在 Soap Web 服务响应中获取 Json 数据

标签 java android json web-services soap

我使用下面的代码在soap webservice响应中获取JSON

SoapSerializationEnvelope envelope = new new SoapSerializationEnvelope(SoapEnvelope.VER11);

try {
    SOAP_ACTION = namespace + MethodName;

    //Adding values to request object
    request = new SoapObject(namespace, MethodName);

    //Adding Double value to request object
    PropertyInfo weightProp =new PropertyInfo();

    //Adding String value to request object
    request.addProperty("myParam1", "" + myParam1);
    request.addProperty("myParam2", "" + myParam2);
    SetEnvelope(url);

    try {             
        //SOAP calling webservice
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //Got Webservice response
        SoapObject res = (SoapObject) envelope.getResponse();
        return Integer.parseInt(res);                 
    } catch (Exception e) {
        return -1;
        //return 0;
    }
} catch (Exception e) {
    return -1;
    //return 2;
}

作为回应,我的 Json 数据是

({"result":"123456" })

然后我上网查了一下我的Json,不对然后我转换了我的JSON数据

{"result":"123456" }

但是在这两种情况下我都得到了 异常

 12-27 13:49:58.905: I/Webservice(2196):  unexpected type (position:TEXT [{"result":"" }]@1:16 in java.io.InputStreamReader@406d5c48) 

最佳答案

看起来您正在获取 JSON,那么您为什么要尝试解析 SOAP? 我发现处理其他纯 JSON 响应很复杂,几乎不可能。

我的服务器端以 SOAP(针对 .NET)和 JSON(针对 Android)进行响应。

这就是我用来从远程服务获取数据并解析它的方法(在本例中为 int)。

//This method receives 2 parameters and return string - just example...
//I'm using HttpGet but there are also HttpPost objects
    public int getResults(String yourParameter1,String yourParameter2)


 {
        int results=0;
        Log.d("Webservice call:","Started");
        //Creating the get URL

        String url= "http://my.webservice.url/targetfile.aspx?parameter1="+yourParameter1+"&parameter2'"+yourParameter2;

        Log.d("URL:",url);
        HttpClient hc = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        String tempresult="";
        Log.d("hc",hc.toString());
        Log.d("post",get.getURI().toString());
         try {
             HttpResponse rp = hc.execute(get);
             Log.d("rp",rp.getEntity().toString());
             Log.d("rp2",rp.getStatusLine().toString());
             // Get hold of the response entity
                HttpEntity entity = rp.getEntity();
                // If the response does not enclose an entity, there is no need
                // to worry about connection release
                tempresult=rp.toString();
                Log.d("tempresult",tempresult);
               if (entity != null) {

                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    tempresult= convertStreamToString(instream);
                    // now you have the string representation of the HTML request
                    instream.close();
                    Log.d("result",tempresult.toString());
                }
                //tempresults holding the JSON 
             JSONObject json = new JSONObject(tempresult);
             //getting the "results" value 
             results=Integer.parseInt(json.getString("result"));

            } catch (Exception e) {
                e.printStackTrace();
                Log.d("error parsing JSON",e.toString());
                        }

         return results;
    }


//This method is to handle response
    private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

现在,请记住您需要 AsyncTask 类来调用此类,特别是如果您想将结果输入 UI 时。

关于java - 在 Soap Web 服务响应中获取 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20795488/

相关文章:

Java 泛型 - 接受 <? 的方法不允许使用 T 类型的对象。延伸 T>

java - 关于广播接收器

java - 如何解析JSON数组对象textview 1和textview 2

Firefox 的 jQuery getJSON 响应为 null,适用于 IE

java - java 问题,String.getBytes 方法

macOS X Lion 上的 Java UI

java - Spark 流输出未保存到 HDFS 文件

android - 如何解决 Android 错误 "method setSupportActionBar in class AppCompatActivity cannot be applied to given types"?

android - 使用 irb() 接口(interface)时出错 - HTTPClient::KeepAliveDisconnected 解析头中 block 中的救援

java - AngularJS 和 Servlet 集成