2016-11-19 82 views
0

所以我對我的天氣應用程序在後臺運行具有的AsyncTask <>的天氣應用的java.net.UnknownHostException

MainActivity

private static final String OPEN_WEATHER_API_URL = "http:/api.openweathermap.org/data/2.5/weather?q=London&APPID=7a65b2d3082a13e6c8ada2cea3a4eea6"; 
... 
... 
WeatherTask task = new WeatherTask(); 
task.execute(OPEN_WEATHER_API_URL); 

WeatherTask

private class WeatherTask extends AsyncTask<String, Void ,WeatherData>{ 

     @Override 
     protected WeatherData doInBackground(String... urls) { 
      Log.v(LOG_TAG,"Executing in background"); 
      if(urls[0] == null || urls.length == 0) 
       return null; 

      WeatherData weather= QueryUtils.fetchWeatherData(urls[0]); 
      return weather; 
     } 

     @Override 
     protected void onPostExecute(WeatherData result) { 
      if(result == null){ 
       return; 
      } 
      updateUi(result); 
     } 
    } 

該課程用於創建URL和提取數據

QueryUtils

public class QueryUtils { 

    private static final String LOG_TAG = QueryUtils.class.getName(); 
    // Private constructor to not instantiate this class 
    private QueryUtils(){ 

    } 

    public static WeatherData fetchWeatherData(String stringUrl) { 
     Log.v(LOG_TAG, " : Strating Fetching data") ; 
     URL url = createUrl(stringUrl); 
     String jsonResponse = null; 

     try{ 
      jsonResponse = makeHttpRequest(url); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     WeatherData weatherData = null; 
     try{ 
     weatherData = extractDataFromJson(jsonResponse); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return weatherData; 
    } 

    private static WeatherData extractDataFromJson(String jsonResponse) throws JSONException { 
     Log.v(LOG_TAG, "Parsing data From JSON File"); 
     Log.v(LOG_TAG , jsonResponse); 

     /* 
      All the Json Parsing here 
      */ 

     return new WeatherData(); 
    } 


    private static String makeHttpRequest(URL url) throws IOException{ 
     Log.v(LOG_TAG, "HTTP REQUEST STARTED"); 
     String jsonResponse = ""; 
     if(url == null){ 
      return jsonResponse; 
     } 
     HttpURLConnection urlConnection = null; 
     InputStream inputStream = null; 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     try{ 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setReadTimeout(10000); 
      urlConnection.setConnectTimeout(15000); 
      urlConnection.connect(); 
      if(urlConnection.getResponseCode() == 200){ 
       inputStream = urlConnection.getInputStream(); 
       jsonResponse = readFromSteram(inputStream); 
      }else{ 
       Log.v(LOG_TAG,"The Response is " + urlConnection.getResponseCode()); 
      } 
     }catch (IOException e){ 
      e.printStackTrace(); 
     }finally { 
      if(urlConnection != null){ 
       urlConnection.disconnect(); 
      } 
      if(inputStream != null){ 
       inputStream.close(); 
      } 
     } 

     return jsonResponse; 
    } 

    private static String readFromSteram(InputStream inputStream) throws IOException { 
     Log.v(LOG_TAG, "Getting Stream"); 
     StringBuilder output = new StringBuilder(); 
     if(inputStream != null){ 
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8")); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
      String line = bufferedReader.readLine(); 
      while(line != null){ 
       output.append(line); 
       line = bufferedReader.readLine(); 
      } 
     } 
     return output.toString(); 
    } 

    private static URL createUrl(String stringUrl) { 
     Log.v(LOG_TAG, "Creating URL"); 
     URL url = null; 
     try{ 
      url = new URL(stringUrl); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     return url; 
    } 
} 

我覺得OPEN_WEATHER_API_URL中的錯誤,但每當我嘗試在Web瀏覽器,它工作正常

,我得到的錯誤:

java.net.UnknownHostException: http:/api.openweathermap.org/data/2.5/weather?q=Tunisia&APPID=7a65b2d3082a13e6c8ada2cea3a4eea6 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.createAddress(HttpEngine.java:1130) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:322) 
W/System.err:  at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:453) 
W/System.err:  at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:123) 
W/System.err:  at com.example.selim.weatherapp.QueryUtils.makeHttpRequest(QueryUtils.java:85) 
W/System.err:  at com.example.selim.weatherapp.QueryUtils.fetchWeatherData(QueryUtils.java:36) 
W/System.err:  at com.example.selim.weatherapp.WeatherActivity$WeatherTask.doInBackground(WeatherActivity.java:36) 
W/System.err:  at com.example.selim.weatherapp.WeatherActivity$WeatherTask.doInBackground(WeatherActivity.java:28) 

許可:

<uses-permission android:name="android.permission.INTERNET" /> 
+0

請加網許可,您的manifest.xml – USKMobility

+0

我已經創建許可 –

+3

您的網址是錯請將http:/以http:// – USKMobility

回答