android - 从 maps.googleapis.com 获取 JSON 数据时出错

标签 android json api android-asynctask

我想解析我从链接下方获取的 JSON

http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false

你可以看到我在浏览器中得到了正确的响应,但是我在从我的代码中得到响应时遇到了以下错误:

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();
    }
}

我知道我肯定在这里犯了一些愚蠢的错误,我也在这里尝试了很多方法,但无法解决。请帮助我摆脱困境。

提前致谢。

最佳答案

如果包含空格字符,请使用 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;

关于android - 从 maps.googleapis.com 获取 JSON 数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479247/

相关文章:

android - 从另一个类访问 onCreate 中的 ArrayAdapter

javascript - Google 饼图不会填充

java - 解耦 JSON 服务中 unicode 转义导致的安全漏洞?

asp.net-mvc - 使用 ASP.NET MVC 创建 API - 全部在一个项目中,还是两个项目中?

c++ - 使用下标运算符的整数 vector 元素比较失败,但使用 at 函数成功

java - 如何将 ArrayAdapter 适配器放入 ArrayList?

java - Google Maps Android Api v2 方法之间的距离不准确

android - 如何检查 SharedPreferences 中是否存在数据

javascript - 从 NodeJS/Express 发送 JSON 响应

api - 什么是方法 API 测试?