获取纬度和经度会给我NetworkOnMainThreadException

时间:2013-06-13 22:53:43

标签: android json networkonmainthread

我试图通过输入字符串并将其发送到谷歌地理编码并使用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;
    }   
}

logcat的

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

2 个答案:

答案 0 :(得分:2)

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

示例:(来自Android Developers

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

答案 1 :(得分:1)

错误日志中的重要一行是:

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

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

请参阅Victor Lap的答案,了解如何正确提出请求。

我还强烈建议您对查询进行正确的URL编码 - “空格”不是唯一可能导致问题的字符......