天气App java.net.UnknownHostException

时间:2016-11-19 18:04:46

标签: java android openweathermap

所以我正在使用AsyncTask<>处理我的天气应用程序在背景

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 中的错误,但每当我在网络浏览器上尝试它都可以正常工作

我得到的错误:

    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" />

1 个答案:

答案 0 :(得分:1)

只需更新

  

private static final String OPEN_WEATHER_API_URL =   &#34; HTTP:/api.openweathermap.org/data/2.5/weather Q =伦敦&安培; APPID = 7a65b2d3082a13e6c8ada2cea3a4eea6&#34 ;;

  

private static final String OPEN_WEATHER_API_URL =   &#34; http://api.openweathermap.org/data/2.5/weather?q=London&APPID=7a65b2d3082a13e6c8ada2cea3a4eea6&#34 ;;