Reputation: 6839
I am attempting to have my action bar complete a search to an api and parse the json results.
I put together my asycTask like this:
private class ReadJSONResult extends AsyncTask
<String, Void, String> {
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result) {
try {
///get
JSONObject jsonObject = new JSONObject(result);
JSONObject weatherObservationItems =
new JSONObject(jsonObject.getString("JSON"));
Toast.makeText(getBaseContext(),
weatherObservationItems.getString("totalResults"),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.d("ReadWeatherJSONFeedTask", e.getLocalizedMessage());
}
}
}
//gets the json from the inputed url
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
I am trying to learn how json parsing works so I attempted to read the json from my api and return the "totalResults" value in a toast to see if I am attempting this correctly.
An example of my JSON looks like this:
My error log in netbeans is:
06-13 13:51:43.228: D/libEGL(9944): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
06-13 13:51:43.244: D/libEGL(9944): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
06-13 13:51:43.267: D/libEGL(9944): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
06-13 13:51:43.431: D/OpenGLRenderer(9944): Enabling debug mode 0
06-13 13:51:51.548: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:51.548: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:52.322: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:52.322: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:56.306: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:56.306: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:58.064: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:58.064: E/SpannableStringBuilder(9944): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-13 13:51:58.220: W/dalvikvm(9944): threadid=11: thread exiting with uncaught exception (group=0x40d4a930)
06-13 13:51:58.267: E/AndroidRuntime(9944): FATAL EXCEPTION: AsyncTask #1
06-13 13:51:58.267: E/AndroidRuntime(9944): java.lang.RuntimeException: An error occured while executing doInBackground()
06-13 13:51:58.267: E/AndroidRuntime(9944): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-13 13:51:58.267: E/AndroidRuntime(9944): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.lang.Thread.run(Thread.java:856)
06-13 13:51:58.267: E/AndroidRuntime(9944): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 114: http://api.brewerydb.com/v2/search?key=(API key goes here)&format=json&type=beer&withBreweries=y&q=90
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.net.URI.create(URI.java:727)
06-13 13:51:58.267: E/AndroidRuntime(9944): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
06-13 13:51:58.267: E/AndroidRuntime(9944): at com.example.beerportfoliopro.MainActivity.readJSONFeed(MainActivity.java:96)
06-13 13:51:58.267: E/AndroidRuntime(9944): at com.example.beerportfoliopro.MainActivity$ReadJSONResult.doInBackground(MainActivity.java:70)
06-13 13:51:58.267: E/AndroidRuntime(9944): at com.example.beerportfoliopro.MainActivity$ReadJSONResult.doInBackground(MainActivity.java:1)
06-13 13:51:58.267: E/AndroidRuntime(9944): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-13 13:51:58.267: E/AndroidRuntime(9944): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-13 13:51:58.267: E/AndroidRuntime(9944): ... 4 more
Upvotes: 0
Views: 343
Reputation: 2186
Definitely you have extra spaces at the end of your url. I can reproduce your error in Java with this code:
import java.net.URI;
public class Main {
public static void main(String[] argv) {
URI uri = URI.create("http://api.brewerydb.com/v2/search?key=(key goes here)&format=json&type=beer&withBreweries=y&q=90 ");
System.out.println(uri.toString());
}
}
And the result is:
Exception in thread "main" java.lang.IllegalArgumentException
at java.net.URI.create(URI.java:841)
at Main.main(Main.java:6)
Caused by: java.net.URISyntaxException: Illegal character in query at index 114: http://api.brewerydb.com/v2/search?key=(Key goes here)&format=json&type=beer&withBreweries=y&q=90
at java.net.URI$Parser.fail(URI.java:2810)
at java.net.URI$Parser.checkChars(URI.java:2983)
at java.net.URI$Parser.parseHierarchical(URI.java:3073)
at java.net.URI$Parser.parse(URI.java:3015)
at java.net.URI.<init>(URI.java:577)
at java.net.URI.create(URI.java:839)
... 1 more
A simple URI.create(urls[0].trim()) will solve.
Upvotes: 1
Reputation: 2889
It says there is an illegal character at index 114, that seems to be at the end of your request. Is it possible there is an added space at the end? Maybe try printing in logcat something like
Log.d("http_string_test", "[" + URL + "]");
See if there is a space before the ] and if so try to reduce that from your call.
Upvotes: 5
Reputation: 2759
Try encoding your URL with the URLEncoder.encode(String url)
.
It might solve your problem
Upvotes: 2