When I try the URL without spaces, it works perfectly. However, when I add a space, it gives me the following error:
Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 79: http://api.wunderground.com/api/81eb946ab954bdcb/hourly/lang/q/Canada/Bachelors Island Marine.json
When I tried it in the browser, it works.
PS: I tried encoding the URL and the same error occurred.
CODE:
package com.example.elie.myapplication;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* Async task class to get json by making HTTP call
* */
public class GetWeatherInfo extends AsyncTask<Void, Void, Void> {
private String mCity;
private OnTaskComplete onTaskComplete;
private ProgressDialog pDialog;
// URL to get Country
private static String url ;
// JSON Node names
private static final String TAG_FORECAST="hourly_forecast";
private static final String TAG_CONDITION="condition";
private static final String TAG_HOUR="hour";
private static final String TAG_DATE="pretty";
private static final String TAG_FCTTIME="FCTTIME";
private static final String TAG_HUMIDITY="humidity";
private static final String TAG_UVI="uvi";
//The calling Activity
private Activity mActivity;
private WeatherInfo mWeather;
//Country name
private String mCountry;
private JSONArray mForecast;
//Constructor
public GetWeatherInfo(Activity act,String country,String city) {
mWeather=new WeatherInfo();
mCountry=country;
mCity=city;
mActivity=act;
url = "http://api.wunderground.com/api/81eb946ab954bdcb/hourly/lang/q/"+country+"/"+mCity+".json";
url.replaceAll(" ","%20");
Log.d("URL",url);
this.execute();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(mActivity);
pDialog.setMessage("Getting weather conditions...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
mWeather.setCityName(mCity);
mWeather.setCountryName(mCountry);
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
mForecast = jsonObj.getJSONArray(TAG_FORECAST);
// 1st element JSON object
JSONObject firstElement =mForecast.getJSONObject(0);
// FCTTIME JSON object
JSONObject FCTTIME =firstElement.getJSONObject(TAG_FCTTIME);
mWeather.setHour24(Integer.parseInt(FCTTIME.getString(TAG_HOUR)));
mWeather.setDate(FCTTIME.getString(TAG_DATE));
mWeather.setHumidity(Integer.parseInt(firstElement.getString(TAG_HUMIDITY)));
mWeather.setUVIndex(Integer.parseInt(firstElement.getString(TAG_UVI)));
mWeather.setCondition(firstElement.getString(TAG_CONDITION));
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
onTaskComplete.setMyTaskComplete(mWeather);
}
public String getCountry()
{
return mCountry;
}
public interface OnTaskComplete {
public void setMyTaskComplete(WeatherInfo message);
}
public void setMyTaskCompleteListener(OnTaskComplete onTaskComplete) {
this.onTaskComplete = onTaskComplete;
}
}
ERROR:
02-12 18:43:04.098 5547-6459/com.example.elie.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 80: http://api.wunderground.com/api/81eb946ab954bdcb/hourly/lang/q/Canada/Addenbroke Island.json at java.net.URI.create(URI.java:727) at org.apache.http.client.methods.HttpGet.(HttpGet.java:75) at com.example.elie.myapplication.ServiceHandler.makeServiceCall(ServiceHandler.java:68) at com.example.elie.myapplication.ServiceHandler.makeServiceCall(ServiceHandler.java:34) at com.example.elie.myapplication.GetWeatherInfo.doInBackground(GetWeatherInfo.java:77) at com.example.elie.myapplication.GetWeatherInfo.doInBackground(GetWeatherInfo.java:18) at android.os.AsyncTask$2.call(AsyncTask.java:287)
Escape your url. Should be like this: http://api.wunderground.com/api/81eb946ab954bdcb/hourly/lang/q/Canada/Bachelors%20Island%20Marine.json
Supposing you have a String as your url , you could do:
url.replaceAll(" " ,"%20");
EDIT
As many suggested, the most correct approach would be:
url = URLEncoder.encode(url, "UTF-8");
To avoid any problems like these in the future.