我试图让我的操作栏完成对api的搜索并解析json结果。
我把asycTask放在一起像这样:
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();
}
我正在尝试学习json解析是如何工作的,所以我试图从我的api读取json并在toast中返回“totalResults”值,看看我是否正确地尝试了这个。
我的JSON示例如下:
我在netbeans中的错误登录是:
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
答案 0 :(得分:5)
它说索引114处有一个非法字符,似乎是在您的请求结束时。是否有可能在最后增加空间?也许尝试在logcat中打印像
这样的东西Log.d("http_string_test", "[" + URL + "]");
查看]之前是否有空格,如果是,请尝试减少通话中的空格。
答案 1 :(得分:2)
尝试使用URLEncoder.encode(String url)
对您的网址进行编码。
它可能会解决您的问题
答案 2 :(得分:1)
您的网址末尾肯定有额外的空格。我可以使用以下代码在Java中重现您的错误:
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());
}
}
结果是:
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
一个简单的URI.create(urls [0] .trim())将解决。