2015-10-19 78 views
1

我无法让我的应用程序打开到在线JSON文件的URL的连接。我遵循指导如何获得inBackground线程来获取URL,但是当我在该URL上调用.connect()时,它似乎返回inBackground函数,因为我测试的只是一个将被修改并显示为Toast,并在httpConn.connect()之后立即修改字符串根本没有任何改变。我确信我的权限在清单中是正确的,但也许有一些我忽略的东西。HTTP URL连接Android

protected String doInBackground(URL... urls){ 

     InputStream in = null; 
     String result = "test"; 
     int responseCode = -1; 
     try { 
      URL url = urls[0]; 
      URLConnection urlConn = url.openConnection(); 
      if (!(urlConn instanceof HttpURLConnection)) { 
       throw new IOException("URL is not an Http URL"); 
      } 
      HttpURLConnection httpConn = (HttpURLConnection) urlConn; 
      httpConn.setRequestMethod("GET"); 
      httpConn.setDoInput(true); 

      result = "get here"; 
      httpConn.connect(); 
      result = "don't get here"; 

      responseCode = httpConn.getResponseCode(); 
      if(responseCode == HttpURLConnection.HTTP_OK){ 
       in = httpConn.getInputStream(); 
      } 
      httpConn.disconnect(); 
      result = readIt(in, 10); 
      return result; 
     } 
     catch (Throwable t){ 
      t.printStackTrace(); 
     } 
     return result; 
    } 

是什么原因造成有不被任何连接或如何测试,如果我只是远眺或没有充分理解这一点任何想法?如果需要,我可以额外添加代码谢谢

+0

请发布'AsyncTask'类代码。 – Hasanaga

回答

1

因为我越来越,你想连接到一个web url,它返回json响应。我是吗?如果是,那么你需要访问this链接以获得完整的解决方案。

+0

谢谢!正是我所期待的,我会在今天晚些时候尝试一下。 –

+0

@ChrisCode你有没有尝试过以上的解决方案。 –

+0

我得到它的工作,我的问题是,我没有正确执行我的AsyncTask。 –