如何在android中使用Dome解析器

时间:2014-04-29 10:38:30

标签: java android

http://www.webservicex.net/country.asmx?op=GetISD这是我要解析的网络服务,如果通过印度那么它应该返回代码。

public String CountryName(String Country)
    {
        HttpClient httpclient=new DefaultHttpClient();
        HttpGet htpget=new HttpGet("http://www.webservicex.net/country.asmx?op=GetISD");

        try {
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(htpget);
            String resp = response.getStatusLine().toString();
            Toast.makeText(this, resp, 5000).show();


            } catch (ClientProtocolException e) {
            Toast.makeText(this, "Error", 5000).show();
            } catch (IOException e) {
            Toast.makeText(this, "Error", 5000).show();
            }
        return code;

    }

我收到响应代码200但是我无法做Dom解析请帮助我如何实现我将如何从DOm Parsing获取代码。

System.InvalidOperationException: Request format is invalid: .
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

此错误即将来临

3 个答案:

答案 0 :(得分:0)

http://developer.android.com/reference/org/apache/http/HttpResponse.html

你只使用.getStatusLine() - 它不仅仅是response.toString()?

答案 1 :(得分:0)

我建议使用ksoap2。解析响应的代码如下所示:

HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
parseResponse(is, envelope);

并且响应将在envelope.bodyIn property中 - 它与json对象非常相似

private static void parseResponse(InputStream is, SoapEnvelope envelope)
          throws Throwable {
        try {
          XmlPullParser xp = new KXmlParser();
          xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);                          

          xp.setInput(is, "UTF-8");

          envelope.parse(xp);
        } catch (Throwable e) {           
            Log.e(LOG_TAG, "Error reading/parsing SOAP response", e);
            throw e;
        }
      }

答案 2 :(得分:0)

您可以使用以下网址来使用简单的http请求:

" http://www.webservicex.net/country.asmx/GetISD?CountryName=&#34 + yourCountryName

它直接提供XML响应。