2015-10-19 76 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。 –