2016-07-26 22 views
0

嘗試使用來自同一類的其他URL時出現問題。我將訪問類似於以下許多次的網頁。如何在Java/Android中的AsyncTask中訪問不同的URL

我查了很多不同的Q/A和教程,似乎無法找到如何傳遞一個URL作爲參數,所以它在doInBackground方法中使用。我調用execute方法,例如:

new RetrieveFeedTask().execute(); 
private class RetrieveFeedTask extends AsyncTask<Void, Void, String> { 

    protected void onPreExecute() { 
    } 

    protected String doInBackground(Void... urls) { 
     try { 
      URL url = new URL(API_URL_COMPETITIONS); // this is url http://api.football-data.org/v1/competitions 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      try { 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReadelConnection.getInputStream())); 
       StringBuilder stringBuilder = new StringBuilder(); 
       String line; 
       while ((line = bufferedReader.readLine()) != null) { 
        stringBuilder.append(line).append("\n"); 
       } 
       bufferedReader.close(); 
       return stringBuilder.toString(); 
      } finally { 
       urlConnection.disconnect(); 
      } 
     } catch (Exception e) { 
      Log.e("ERROR", e.getMessage(), e); 
      return null; 
     } 
    } 

    //entire JSON page is saved in response 
    protected void onPostExecute(String response) { 
     if(response == null) { 
      response = "THERE WAS AN ERROR"; 
     } 
     Log.i("INFO", response); 
     try { 
      JSONArray comps = new JSONArray(response); 
      JSONObject second = comps.getJSONObject(1); //this is array element PremierLeague2016/2017 
      compArray.add("Competition"); 
      compArray.add(second.getString("caption")); 
      //i then set this to an ArrayAdapter and show the results in a Spinner.     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      competition.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

感謝任何幫助!

+0

我很困惑問題是什麼。你需要替換'API_URL_COMPETITIONS'並使用你想要的任何URL –

+0

是的,但是我只需要這個URL用於一個Spinner,然後另一個Spinner用不同的URL,等等。所以我需要創建這個類的實例,但每次都有不同的URL,如果可能的話,但不知道我該怎麼做。 – Lewisfitz07

+0

再次感謝您使用Volley庫和'JsonObjectRequest'或'JsonArrayRequest' –

回答

0

似乎無法找到如何通過URL作爲因此它用在doInBackground方法中

AsyncTask類是AsyncTask<Params, Progress, Result>。您需要String參數,而不是Void參數。

private class RetrieveFeedTask extends AsyncTask<String, Void, String> 

然後你可以使用protected String doInBackground(String... urls)(因爲虛空的網址是沒有意義的)

和更新您的通話

String url1 = "http://..."; 
String url2 = "http://..."; 
new RetrieveFeedTask().execute(url1, url2); 

而且在doInBackground

for (String url : urls) { 
    try { 
     URL url = new URL(url); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

或者,忘記循環...您可以簡單地訪問urls[0]以獲取第一個字符串val UE,例如,只是要小心

+0

謝謝你的工作! – Lewisfitz07

0

你需要改變你的AsyncTask接收字符串(URL)作爲參數:

//call 
String myURL = "http://....."; 
new RetrieveFeedTask().execute(myURL); 

... 

//change parameter type in declaration 
private class RetrieveFeedTask extends AsyncTask<String, Void, String> { 

    protected String doInBackground(String... urls) { 
     try { 
      // get the parameter 
      URL url = new URL(urls[0]); 
      ... 

https://developer.android.com/reference/android/os/AsyncTask.html

0

在你的異步任務類你的第一個Void需要在參數中的IndexOutOfBoundsException異常的:

new RetrieveFeedTask().execute(url); 

private class RetrieveFeedTask extends AsyncTask<URL, Void, String> { 

通過將URL調用它獲取異步任務中的網址:

protected String doInBackground(URL... urls) { 
    URL url = urls[0]; 

如果你想通過多個網址,只要沿着通過他們:

new RetrieveFeedTask().execute(url1, url2); 

...和檢索它們:

protected String doInBackground(URL... urls) { 
    URL url1 = urls[0]; 
    URL url2 = urls[1]; 

而且,看看在documentation

相關問題