I'm trying to do a simple weather app that will allow a user to input their city and it will return weather data. I'm using a fragment that has the EditText and I'm using an interface to pass the string to the MainActivity, which will need to concat in the URL string and this is where the issue comes.Getting an exception error, I believe protocol not found. I did check my URL that http:or https was input. Any suggestions passing the URL to my AsyncTask
//Interface method
@Override
public void displayCityChosen(String cityName) {
Log.i(TAG, "city name= " +cityName);
String weatherURL = "http://www.api.openweathermap.org/data/2.5/weather?q=" + cityName + "&APPID=32c831d8a6937f237acff8eef3d4a58c";
try{
String base = URLEncoder.encode(weatherURL,"UTF-8");
URL finalWeatherURL = new URL(base);
new GetWeather().execute(finalWeatherURL);
}catch (Exception e) {
Log.i(TAG, "ERROR");
}
}
private class GetWeather extends AsyncTask<URL, Void, JSONObject> {
@Override
protected JSONObject doInBackground(URL... urls) {
int statusCode = -1;
String jsonString = "";
for (URL queryURL : urls) {
try {
URLConnection conn = queryURL.openConnection();
jsonString = IOUtils.toString(conn.getInputStream());
jsonResponse = new JSONObject(jsonString);
jsonArray = jsonResponse.getJSONArray("weather");
Log.i(TAG, "Array= " + jsonArray);
break;
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonResponse;
}
}
I think you need one more step to change the URL to a HttpURLConnection and I don't think this url your using needs to be encoded. all the encode does is mess up the slashes.
FYI pass the string to the asynctask and do all the conversion in the asynctask it will make for a more responsive app.