我目前正在开始为Android开发应用程序,但我目前仍然遇到了一个未知的问题。
这是我的代码: ...
protected class FetchWeatherTask extends AsyncTask <String, Void, Void>{
private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();
protected Void doInBackground(String... params) {
//initialize Connection and reader
HttpURLConnection urlconnection=null;
BufferedReader reader=null;
String forecastJsonStr =null;
String format = "json";
String units = "metric";
int numDays = 7;
try{
final String BASE_URL = "http://apikey.openweathermap.org/data/2.5/forecast/daily";
final String QUERY_PARAM="zip";
final String FORMAT_PARAM= "mode";
final String UNITS_PARAM= "units";
final String DAYS_PARAM= "cnt";
final String APPID_PARAM="APPID";
//make Connection to the OpenWeatherURL
//IDEA: API-Code for future use as parameter of the method
//String apikey = "&APPID="+BuildConfig.OPEN_WEATHER_MAP_API_KEY;
//String baseUrl="http://apikey.openweathermap.org/data/2.5/forecast/daily?zip=94043&mode=json&units=metric&cnt=7";
//URL url= new URL(baseUrl.concat(apikey));
Uri builtUri = Uri.parse(BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM,params[0])
.appendQueryParameter(FORMAT_PARAM,format)
.appendQueryParameter(UNITS_PARAM,units)
.appendQueryParameter(DAYS_PARAM,Integer.toString(numDays))
.appendQueryParameter(APPID_PARAM,BuildConfig.OPEN_WEATHER_MAP_API_KEY)
.build();
URL url = new URL(builtUri.toString());
//URL url = new URL("http://apikey.openweathermap.org/data/2.5/forecast/daily?zip=94043&mode=json&units=metric&cnt=7&APPID=31d96db6578ca2e94f31714e882118be");
Log.v(LOG_TAG, "Built URI "+ builtUri.toString());
urlconnection= (HttpURLConnection) url.openConnection();
urlconnection.setRequestMethod("GET");
urlconnection.connect();
//Read inputStream into a buffer
InputStream input = urlconnection.getInputStream();
StringBuffer buffer= new StringBuffer();
if(input==null){
return null;
}
reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line=reader.readLine())!=null){
buffer.append(line+"\n");
}
if (buffer.length()==0){
return null;
}
forecastJsonStr=buffer.toString();
}
catch(IOException e){
Log.e("ForecastFragment","Error",e);
return null;
}finally {
if(urlconnection==null){
urlconnection.disconnect();
}
if(reader!=null){
try{
reader.close();
} catch (IOException e) {
Log.e("ForecastFragment","Error",e);
}
}
return null;
}
}
}
我已经在我的Android清单中为此应用授予了互联网访问权限,但它仍然会导致此异常。这里是manifest.xml用于审查目的:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cc_student.sunshine.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>