“ url.openConnection()”不起作用。在Android中

时间:2019-06-06 10:49:48

标签: connection openconnect

我的意图是打开网站中存储的PCONTROL.xml文件,并读取标签和值。 xml用于存储可变数据,例如日期,页数,月份等。

PCONTROL.xml is stored in this link "http://www.geocities.ws/mariyanaatham/PCONTROL.xml". The code will open this file and read the tags. This is only a trial xml. 

public String[]  GetVaules(){
  String[] val = new String[2];
      /** Create a new textview array to display the results
    TextView name[];
    TextView website[];
    TextView category[];*/
    try {

        URL url = new URL("http://www.geocities.ws/mariyanaatham/PCONTROL.xml");
        XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp=factory.newPullParser();
        xpp.setInput(getInputStream(url), "UTF_8");
        int eventType=xpp.getEventType();
        int i=0;

        while(eventType!=XmlPullParser.END_DOCUMENT){
            // Looking for a start tag
            if(eventType==XmlPullParser.START_TAG){
                //We look for "title" tag in XML response
                 if(xpp.getName().equalsIgnoreCase("Y")){
                    //Once we found the "title" tag, add the text it contains to our builder
                    val[i]=xpp.nextText();
                }
                if(xpp.getName().equalsIgnoreCase("M")){
                    //Once we found the "title" tag, add the text it contains to our builder
                    val[i]=xpp.nextText();
                }
            }

            eventType=xpp.next();
        }

    } catch (Exception e) {

        System.out.println("XML Pasing Excpetion = " + e);
    }
  return val;
  }

    private InputStream getInputStream(java.net.URL url) {
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            //return url.openConnection().getInputStream();
            return conn.getInputStream();
        } catch (IOException e) {
            return null;
        }
    }

问题是url.openConnection()无法正常工作。因此不返回getinputStream。 请帮忙。

0 个答案:

没有答案