«url.openConnection ()» не работает. в андроид - PullRequest
0 голосов
/ 06 июня 2019

Я собираюсь открыть файл 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. Пожалуйста, помогите.

...