问题描述:

So i'm working on my weather app with an AsyncTask<> on the background

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);

}

}

This class is for creating URL and fetching data

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;

}

}

I think the error within OPEN_WEATHER_API_URL but whenever I try it on the web browser it works fine

The error that I get :

 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)

permission :

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

网友答案:

just update

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

to

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

相关阅读:
Top