在我的应用程序中,我有一个方法,它接受一个地址字符串并获取有关它的信息,并使用Google Geocode将其转换为JSONObject。方法如下:
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
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();
}
return jsonObject;
}
当我在设备上通过Eclipse运行应用程序时,这很好用。但是,当我制作APK并将其安装在设备上时,应用程序会在上面的代码中崩溃。我的日志cat输出如下:
04-14 21:43:19.677: E/AndroidRuntime(21560): FATAL EXCEPTION: main
04-14 21:43:19.677: E/AndroidRuntime(21560): Process: com.example.carpool, PID: 21560
04-14 21:43:19.677: E/AndroidRuntime(21560): android.os.NetworkOnMainThreadException
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
04-14 21:43:19.677: E/AndroidRuntime(21560): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-14 21:43:19.677: E/AndroidRuntime(21560): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-14 21:43:19.677: E/AndroidRuntime(21560): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-14 21:43:19.677: E/AndroidRuntime(21560): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-14 21:43:19.677: E/AndroidRuntime(21560): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-14 21:43:19.677: E/AndroidRuntime(21560): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-14 21:43:19.677: E/AndroidRuntime(21560): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-14 21:43:19.677: E/AndroidRuntime(21560): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-14 21:43:19.677: E/AndroidRuntime(21560): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-14 21:43:19.677: E/AndroidRuntime(21560): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-14 21:43:19.677: E/AndroidRuntime(21560): at com.example.carpool.Book.getLocationInfo(Book.java:421)
04-14 21:43:19.677: E/AndroidRuntime(21560): at com.example.carpool.Book$8.onItemClick(Book.java:236)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.widget.AbsListView$3.run(AbsListView.java:3638)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.os.Handler.handleCallback(Handler.java:733)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.os.Handler.dispatchMessage(Handler.java:95)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.os.Looper.loop(Looper.java:136)
04-14 21:43:19.677: E/AndroidRuntime(21560): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-14 21:43:19.677: E/AndroidRuntime(21560): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 21:43:19.677: E/AndroidRuntime(21560): at java.lang.reflect.Method.invoke(Method.java:515)
04-14 21:43:19.677: E/AndroidRuntime(21560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-14 21:43:19.677: E/AndroidRuntime(21560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-14 21:43:19.677: E/AndroidRuntime(21560): at dalvik.system.NativeStart.main(Native Method)
崩溃的确切行是response = client.execute(httppost);
行。我不知道为什么会这样,以及如何纠正?
答案 0 :(得分:1)
您正在从Main Thread
访问网络。您只能在后台Thread
像这样使用AsyncTask
new AsyncTask<URL, Integer, Long>(){
@Override
protected Long doInBackground(URL... params) {
try {
// call you method here
} catch (Exception ex) {
// handle the exception here
}
return null;
}
}.execute();
答案 1 :(得分:1)
android.os.NetworkOnMainThreadException
表示您正在主(ui)线程上执行网络。你不能在主线程上执行网络操作.android不允许这样做。这就是崩溃的原因。在asyntask中移动你的http请求,所以它将在单独的线程中运行,你的应用程序不会崩溃。