cant get xml file from an URL in android

2019-05-30 12:42发布

in my app i am trying to get an xml file from an URL. the url to which trying get consists of a string. Following is part of my code

  URL url = new URL("http://..................");
  Log.e("Gallery URl",""+url);
  getWebPageContents(url);
  }

   private void getWebPageContents(URL url) 
   {
     try{
    if(url != null)
    Log.e("webpage",""+url);
    {
          HttpClient client = new DefaultHttpClient();
      HttpGet get = new HttpGet();
      try
         {
        HttpResponse rsp = client.execute(get);
        String tmpstr = EntityUtils.toString(rsp.getEntity()); 
        Log.e("UserData "+tmpstr);
        UserManual.IMGDATA=tmpstr;
        getUserRegisteredContents(tmpstr);
         }
    catch(Exception asd)
       {
         Log.e(asd.toString());
       }            
  }

When i checked with log value in log cat it shows that it is getting in to the URL and then into the getWebPageContents(url)

in the getWebPageContents(URL url) it is getting into the first try block and not in the second try block

Where i am going wrong please help me.....

标签: android http url
2条回答
爷的心禁止访问
2楼-- · 2019-05-30 13:19

You are not setting the url you want to get anywhere so I believe rsp.getEntity() results in a NullPointerException

Also HttpGet accepts a URI or String so either you use it as:

HttpGet get = new HttpGet("http://www.example.com");

or change

URL url = new URL("http://..................");

to

URI url = new URI("http://..................");
查看更多
乱世女痞
3楼-- · 2019-05-30 13:35

You are not using the URL in your code:

HttpGet get = new HttpGet();

I think this is what you want:

URI uri = new URI("http://..................");
HttpGet get = new HttpGet(uri);
查看更多
登录 后发表回答