gpt4 book ai didi

java - 天气应用程序 java.net.UnknownHostException

转载 作者:行者123 更新时间:2023-12-01 09:14:10 29 4
gpt4 key购买 nike

所以我正在开发我的天气应用程序,后台有一个 AsyncTask<>

主要 Activity

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

天气任务

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 和获取数据

查询实用程序

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

最佳答案

刚刚更新

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

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

关于java - 天气应用程序 java.net.UnknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40696223/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com