我想解析我从Link下面得到的JSON
你可以看到我在浏览器中正确获得响应,但是从我的代码获取响应时出现以下错误:
Logcat响应:
01-23 11:54:57.415: E/AndroidRuntime(2190): FATAL EXCEPTION: AsyncTask #1
01-23 11:54:57.415: E/AndroidRuntime(2190): java.lang.RuntimeException: An error occured while executing doInBackground()
01-23 11:54:57.415: E/AndroidRuntime(2190): at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.lang.Thread.run(Thread.java:1019)
01-23 11:54:57.415: E/AndroidRuntime(2190): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 116: http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.net.URI.create(URI.java:776)
01-23 11:54:57.415: E/AndroidRuntime(2190): at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
01-23 11:54:57.415: E/AndroidRuntime(2190): at com.example.gisapp.FirstActivity.connect(FirstActivity.java:155)
01-23 11:54:57.415: E/AndroidRuntime(2190): at com.example.gisapp.FirstActivity$AsynTaskForGettingData.doInBackground(FirstActivity.java:109)
01-23 11:54:57.415: E/AndroidRuntime(2190): at com.example.gisapp.FirstActivity$AsynTaskForGettingData.doInBackground(FirstActivity.java:1)
01-23 11:54:57.415: E/AndroidRuntime(2190): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-23 11:54:57.415: E/AndroidRuntime(2190): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-23 11:54:57.415: E/AndroidRuntime(2190): ... 4 more
01-23 11:55:00.045: E/WindowManager(2190): Activity com.example.gisapp.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405234a0 that was originally added here
01-23 11:55:00.045: E/WindowManager(2190): android.view.WindowLeaked: Activity com.example.gisapp.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405234a0 that was originally added here
01-23 11:55:00.045: E/WindowManager(2190): at android.view.ViewRoot.<init>(ViewRoot.java:258)
01-23 11:55:00.045: E/WindowManager(2190): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-23 11:55:00.045: E/WindowManager(2190): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-23 11:55:00.045: E/WindowManager(2190): at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.Dialog.show(Dialog.java:241)
01-23 11:55:00.045: E/WindowManager(2190): at com.example.gisapp.FirstActivity$AsynTaskForGettingData.onPreExecute(FirstActivity.java:95)
01-23 11:55:00.045: E/WindowManager(2190): at android.os.AsyncTask.execute(AsyncTask.java:391)
01-23 11:55:00.045: E/WindowManager(2190): at com.example.gisapp.FirstActivity.onCreate(FirstActivity.java:49)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-23 11:55:00.045: E/WindowManager(2190): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 11:55:00.045: E/WindowManager(2190): at android.os.Looper.loop(Looper.java:130)
01-23 11:55:00.045: E/WindowManager(2190): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-23 11:55:00.045: E/WindowManager(2190): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 11:55:00.045: E/WindowManager(2190): at java.lang.reflect.Method.invoke(Method.java:507)
01-23 11:55:00.045: E/WindowManager(2190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-23 11:55:00.045: E/WindowManager(2190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-23 11:55:00.045: E/WindowManager(2190): at dalvik.system.NativeStart.main(Native Method)
这是我的FirstActivity代码:
package com.example.gisapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class FirstActivity extends Activity {
// Tag Variable to Easily Detect your Activity inside LogCat While you Are
// Placing your Log
static String TAG = "FirstActivity";
// boolean to Mantain the State of Internet Working or Not Default Set to
// true
boolean net = true;
// Progreess Dialogue to Show User when AsyncTask is Doing Work in
// BackGround
ProgressDialog pd;
// String Variable to Store the Data of your Json Response as String
static String result;
String URL = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.firstactivity);
// Execute the AsyncTask
new AsynTaskForGettingData().execute();
}
// For Converting the Given String in the Formate the Website Link otherwise
// you will get illegal Formate Exception cause this String will not be in a
// formate of URL
public static String convertURL(String str) {
String url = null;
try {
url = new String(str.trim().replace(" ", "%20").replace("&", "%26")
.replace(",", "%2c").replace("(", "%28")
.replace(")", "%29").replace("!", "%21")
.replace("=", "%3D").replace("<", "%3C")
.replace(">", "%3E").replace("#", "%23")
.replace("$", "%24").replace("'", "%27")
.replace("*", "%2A").replace("-", "%2D")
.replace(".", "%2E").replace("/", "%2F")
.replace(":", "%3A").replace(";", "%3B")
.replace("?", "%3F").replace("@", "%40")
.replace("[", "%5B").replace("\\", "%5C")
.replace("]", "%5D").replace("_", "%5F")
.replace("`", "%60").replace("{", "%7B")
.replace("|", "%7C").replace("}", "%7D"));
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
// Asynctask To get Data from Server
public class AsynTaskForGettingData extends AsyncTask<Void, Void, Void> {
// After we have Execute AsynTaskForGettingData the First Method That
// Will Called up is onPreExecute()
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// Show Progress Dialogue to Tell User to Wait For A While untill we
// finishes to get the Data From Server and Parse Json
pd = new ProgressDialog(FirstActivity.this);
pd.setTitle("GISApp");
pd.setMessage("Please Wait..");
pd.show();
}
// After Executing onPreExecute() the Method Will Do work inside
// doInBackground()
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// URL = convertURL(URL);
StringBuffer sb = new StringBuffer();
sb.append("http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false");
URL = sb.toString();
if (connect(URL)) {
try {
JSONObject j_obj = new JSONObject(result);
// msg = j_obj.getString("MESSAGE");
// Log.i(TAG, "Value of MESSAGE" + msg);
/*
* if (msg.equalsIgnoreCase("Password sent")) {
* Toast.makeText( LoginActivity.this,
* "Your Username & Password Has Been Sent to your Email Address"
* , Toast.LENGTH_LONG) .show(); } else if (msg
* .equalsIgnoreCase("fail")) { Toast.makeText(
* LoginActivity.this, "Please Enter Valid Email Address",
* Toast.LENGTH_LONG) .show(); }
*/
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// msg = "error";
}
return null;
}
// After Finishing Work doInBackground() the Method Will execute its
// onPostExecute method
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
}
}
// Method That Will Check Whether the Internet is Working or Not and If
// Working it will Set the Response inside result Variable.
@SuppressWarnings("finally")
public static boolean connect(String url) {
boolean flag = false;
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
Log.e("url", url);
// Execute the request
HttpResponse response;
try {
// https://api.vkontakte.ru/method/audio.search?uid=163398985&q=akoncount=100&access_token=2a4db0e223f0f5ab23f0f5ab5f23da5680223f023f1f5a3c696b018be9b17b9
response = httpclient.execute(httpget);
response.addHeader("Accept-Language", "zh-CN");
// Examine the response status
Log.i(TAG, response.getStatusLine().toString() + "\n" + response);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
Log.d(TAG, "---- instream --- " + instream);
result = convertStreamToString(instream);
Log.d(TAG, "---- Result --- " + result);
// now you have the string representation of the HTML request
instream.close();
} else {
Log.d(TAG, "---- Json Activity is --- null ");
}
flag = true;
// net = false;
} catch (Exception e) {
Log.d("Jsomn Activity", "---- Catch --- " + e.toString());
flag = false;
e.printStackTrace();
} finally {
return flag;
}
}
// Method That Will Convert your InputStream to String
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("Jsomn Activity", "---- Result --- " + sb.toString());
return sb.toString();
}
}
我知道我肯定在这里做了一些愚蠢的错误我也在这里试过很多方法但是无法解决它。请帮我摆脱这个。
先谢谢。
答案 0 :(得分:3)
如果网址包含空格字符,请使用URLEncoder.encode
对您的网址进行编码:
StringBuffer sb = new StringBuffer();
sb.append("origin=Chicago,IL&destination=Los+Angeles,CA&
waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false");
URL = URLEncoder.encode(sb.toString(), "UTF-8");
String newURL = "http://maps.googleapis.com/maps/api/directions/json?"+URL;
答案 1 :(得分:0)
收到回复后才解雇progressdialog
// After Finishing Work doInBackground() the Method Will execute its // onPostExecute method @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); if(pd.isShowing()) pd.dismiss(); }