我试图通过提供一个字符串并将其发送到谷歌地理编码并用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