有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java的安卓 XML解析没有得到任何响应

我试图从URL获取XML文件,但没有得到响应,代码稍后停止,因为字符串XML为空,您能告诉我问题出在哪里吗

public String getXmlFromUrl(String url) {
 String xml = null;

    try {


        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
                    // I printed the response here but I got nothing ! 
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
                    return xml;


    } catch (Exception e) {
        e.printStackTrace();
    }

请具体回答我感谢你的帮助


共 (5) 个答案

  1. # 1 楼答案

    你应该理解例外情况。xml之所以null是因为“某些东西”出了问题。这引发了一场Exception,可能对出错的地方进行了很好的描述。当这种情况发生时,Exception被抛出,直到有人处理它

    Exception的每个子类都有不同的“味道”,在特定情况下抛出。这使您能够对错误做出“反应”。例如,你可以告诉用户出了什么问题,或者为了调试而记录一些东西

    在您的例子中,您可以在一个地方“捕获”所有异常,当异常发生时,执行catch (Exception e)之后的代码。你在这里什么都不做,只是打印出一些东西(这会在你的LogCat中显示为橙色)。然后你继续,好像什么都没发生。但是xml将是空的,这对您的程序不利,而且您显然没有注意到LogCat条目,因为您的程序在稍后崩溃

    这一次,艾尔德姆·巴布解决了你的问题。下一次,当出现其他错误时(htttp请求中可能会出现很多错误),您的程序将显示相同的行为。试着仔细阅读例外情况,当你处理它们时要小心

  2. # 2 楼答案

    为什么要使用HTTPPost??你甚至没有发送任何数据。试试HttpGet

    试试这个:

    public String getXmlFromUrl(String url) throws Exception {
        return new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... params) {
                String xml = null;
                try {
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpPost = new HttpGet(params[0]);
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    // I printed the response here but I got nothing !
                    HttpEntity httpEntity = httpResponse.getEntity();
                    xml = EntityUtils.toString(httpEntity);
                    Log.i("DEMO", xml);
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return xml;
            }
        }.execute(url).get();
    
    }
    
  3. # 3 楼答案

    试着从不同的线程开始你的代码

     new Thread(new Runnable()
     {
    
        @Override
        public void run()
        {
            // TODO your code here
    
        }
      }).start();
    
  4. # 4 楼答案

    对于geturldata()

    public InputStream getUrlData(String url) throws URISyntaxException,
        ClientProtocolException, IOException {
    
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet method = new HttpGet(new URI(url));
    HttpResponse res = client.execute(method);
    return res.getEntity().getContent();
    

    }

  5. # 5 楼答案

    试试这个:

    try {
    
        items = new ArrayList<String>();
    
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new InputStreamReader(
                getUrlData(" url")));
    
        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            Log.i(TAG, "doc started");
            if (xpp.getEventType() == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("entry")) {
                    items.add(xpp.getAttributeValue(0));
                }
            }
            xpp.next();
    
        }
    } catch (Throwable t) {
        Toast.makeText(this, "Request failed: " + t.toString(),
                Toast.LENGTH_LONG).show();
    }