为什么将网络访问放到另一个线程之后还是出现android.os.NetworkOnMainThreadException异常

小小白龙马 2014-03-27 10:28:30
package dragon.location.google;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.gson.Gson;

public class MainActivity extends Activity {
private Button geocodingButton = null;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle data = msg.getData();
String val = data.getString("value");
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(geocodingThread).start();
}

Runnable geocodingThread = new Runnable() {
@Override
public void run() {
geocodingButton = (Button)findViewById(R.id.geoButton);
geocodingButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//针对上一节中Geocoder服务不可用的情况,此处使用google maps中的服务替代
String url = "http://maps.google.com/maps/api/geocode/json?address=SFO&sensor=false";
HttpClient client = new DefaultHttpClient();
String resonseData = "";
try {
//向指定的url发送http请求
HttpResponse response = client.execute(new HttpGet(url));
//取得服务器返回的响应
HttpEntity entity = response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = "";
while((line =br.readLine() ) != null){
resonseData = resonseData + line;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
TestResult testResult = gson.fromJson(resonseData,TestResult.class);
System.out.println(resonseData);
System.out.println(testResult);
}
});
Message msg = new Message();
Bundle data = new Bundle();
data.putString("value", "result");
msg.setData(data);
handler.sendMessage(msg);
}
};

/* public static float GetSystemVersion() {
String strVer = android.os.Build.VERSION.RELEASE;
strVer = strVer.substring(0, 3).trim();
float fv = Float.valueOf(strVer);
return fv;
}*/
//将网络操作放在主线程的一种方法:将下面一段话放在网络操作前面
/*
if (GetSystemVersion() > 2.3) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
.build());
}*/
}
...全文
114 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
onClick方法会在主线程中的,所以你应该把线程包在网络请求的外面,onclick的里面。 而不是开个线程去找按钮,并给按钮加事件
哎,真难 2014-03-28
  • 打赏
  • 举报
回复
异步线程这样的写的么,这样写Hand还是绑定了主线程的,,,
iltgcl 2014-03-28
  • 打赏
  • 举报
回复
setOnClickListener中的onClick回調是在主線程裏運行的
小小白龙马 2014-03-28
  • 打赏
  • 举报
回复
引用 4 楼 cclovescw 的回复:
onClick方法会在主线程中的,所以你应该把线程包在网络请求的外面,onclick的里面。 而不是开个线程去找按钮,并给按钮加事件
非常感谢,调通了!
小小白龙马 2014-03-28
  • 打赏
  • 举报
回复
引用 3 楼 heaimnmn 的回复:
异步线程这样的写的么,这样写Hand还是绑定了主线程的,,,
嗯,现在明白了
小小白龙马 2014-03-28
  • 打赏
  • 举报
回复
引用 2 楼 iltgcl 的回复:
setOnClickListener中的onClick回調是在主線程裏運行的
多谢,明白了!
小小白龙马 2014-03-27
  • 打赏
  • 举报
回复
用StrictMode和在Android2.3上都没有问题,猜测是子线程有问题,但不知道哪里出错,求解。。。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧