-1

我试图通过提供一个字符串并将其发送到谷歌地理编码并用JSON取回来获取经度和纬度。但是每当我运行这段代码时,我都会得到一个NetworkOnMainThreadException. 这是为什么?我该如何解决这个问题?

public class SearchAddressActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_on_address);

        getLocationInfo("New Cross rd SE146AS London");
    }


    public JSONObject getLocationInfo(String address) {

        address = address.replaceAll(" ", "%20");
        Double lon = new Double(0);
        Double lat = new Double(0);
        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="+ address + "&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            lon = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lng");

            lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lat");

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.e("LON", String.valueOf(lon));
        Log.e("LAT", String.valueOf(lat));
        return jsonObject;
    }   
}

日志猫

06-14 00:38:21.675: E/AndroidRuntime(18999): FATAL EXCEPTION: main
06-14 00:38:21.675: E/AndroidRuntime(18999): java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.mysite.www/eu.mysite.www.SearchAddressActivity}: android.os.NetworkOnMainThreadException
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.os.Looper.loop(Looper.java:137)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.ActivityThread.main(ActivityThread.java:4898)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at java.lang.reflect.Method.invoke(Method.java:511)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at dalvik.system.NativeStart.main(Native Method)
06-14 00:38:21.675: E/AndroidRuntime(18999): Caused by: android.os.NetworkOnMainThreadException
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at eu.mysite.www.SearchAddressActivity.getLocationInfo(SearchAddressActivity.java:47)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at eu.mysite.www.SearchAddressActivity.onCreate(SearchAddressActivity.java:32)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.Activity.performCreate(Activity.java:5206)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
06-14 00:38:21.675: E/AndroidRuntime(18999):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
06-14 00:38:21.675: E/AndroidRuntime(18999):    ... 11 more
4

2 回答 2

2

错误说明了一切,您不应该在主线程上执行网络操作。
阅读有关进程和线程以及如何使用它们
页面以及有关如何在不同线程上正确使用网络的页面

示例:(来自Android 开发人员

 // Uses AsyncTask to create a task away from the main UI thread. This task takes a 
 // URL string and uses it to create an HttpUrlConnection. Once the connection
 // has been established, the AsyncTask downloads the contents of the webpage as
 // an InputStream. Finally, the InputStream is converted into a string, which is
 // displayed in the UI by the AsyncTask's onPostExecute method.
 private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        // params comes from the execute() call: params[0] is the url.
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        textView.setText(result);
   }
}
于 2013-06-13T22:58:46.173 回答
1

错误日志中的重要行是:

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)

它说“某些东西”正在阻止这个请求 - 特别是操作系统(感谢 Brian Roach 明确表示)。您必须明确地将网络请求放在与 UI 线程不同的线程中 - 否则您的 UI 将挂起,直到网络响应。

有关如何正确提出请求的示例,请参阅 Victor Lap 的答案。

我还敦促您正确地对您的查询进行 URL 编码 - “空格”不是唯一可能导致麻烦的字符......

于 2013-06-13T22:57:21.143 回答