Android 如何使用主线程解决问题
我尝试从php文件中获取JSON数据Android 如何使用主线程解决问题,android,json,multithreading,network-connection,Android,Json,Multithreading,Network Connection,我尝试从php文件中获取JSON数据 public void connect(){ System.out.println("%%%%%%%%%%%%%%%%%1" ); Thread t = new Thread(){ @Override public void run() { try { System.out.println("%%%%%%%%%%%%%%%%%2" ); HttpParams par
public void connect(){
System.out.println("%%%%%%%%%%%%%%%%%1" );
Thread t = new Thread(){
@Override
public void run() {
try {
System.out.println("%%%%%%%%%%%%%%%%%2" );
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 0);
HttpClient httpClient = new DefaultHttpClient(params);
String urlString = "http://url";
//prepare the HTTP GET call
HttpGet httpget = new HttpGet(urlString);
//get the response entity
HttpEntity entity = httpClient.execute(httpget).getEntity();
System.out.println("%%%%%%%%%%%%%%%%%3" );
if (entity != null) {
//get the response content as a string
String response = EntityUtils.toString(entity);
//consume the entity
entity.consumeContent();
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
//return the JSON response
JSONArray jsonarray = new JSONArray(response);
JSONObject jb =(JSONObject) jsonarray.getJSONObject(0);
String name= jb.getString("name");
String punkt = jb.getString("punktezahl");
//String name = jsonarray.getString("name");
System.out.println("HEEEEEEEEEEEEEEEEEEEEEEEEEEEE" + name);
fuehrender.setText(name);
punkte.setText(punkt);
}
}catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
如果我这样做,我得到的信息是,只有创建视图层次结构的原始线程才能接触其视图
因此,由于此错误消息,我尝试如下操作:
public void connect(){
System.out.println("%%%%%%%%%%%%%%%%%1" );
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
System.out.println("%%%%%%%%%%%%%%%%%2" );
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 0);
HttpClient httpClient = new DefaultHttpClient(params);
String urlString = "http://url";
//prepare the HTTP GET call
HttpGet httpget = new HttpGet(urlString);
//get the response entity
HttpEntity entity = httpClient.execute(httpget).getEntity();
System.out.println("%%%%%%%%%%%%%%%%%3" );
if (entity != null) {
//get the response content as a string
String response = EntityUtils.toString(entity);
//consume the entity
entity.consumeContent();
// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
//return the JSON response
JSONArray jsonarray = new JSONArray(response);
JSONObject jb =(JSONObject) jsonarray.getJSONObject(0);
String name= jb.getString("name");
String punkt = jb.getString("punktezahl");
//String name = jsonarray.getString("name");
System.out.println("HEEEEEEEEEEEEEEEEEEEEEEEEEEEE" + name);
fuehrender.setText(name);
punkte.setText(punkt);
}
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
现在,我得到了
NetworkOnMainThread
错误消息。。如何打破这一厄运循环?您有一个runOnUiThread
。移除它。它应该只用于更新ui,而不用于http get请求
使用AsyncTask
是一个更好的选择。在doInBackground
中发出http get请求,并解析响应。您可以在doInbackground
中返回结果,这是onPostExecute
的一个参数
因此,您可以在ui线程上调用的onPostExecute
中更新ui
例如:
援引
new TheTask().execute(); // in ui thread
使AsyncTask
成为活动的内部类
class TheTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... params1) {
String response = null;
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 0);
HttpClient httpClient = new DefaultHttpClient(params);
String urlString = "http://url";
HttpGet httpget = new HttpGet(urlString);
HttpEntity entity = httpClient.execute(httpget).getEntity();
response = EntityUtils.toString(entity);
httpClient.getConnectionManager().shutdown();
}
catch(Exception e)
{
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
JSONArray jsonarray = new JSONArray(result;
JSONObject jb =(JSONObject) jsonarray.getJSONObject(0);
String name= jb.getString("name");
String punkt = jb.getString("punktezahl");
fuehrender.setText(name);
punkte.setText(punkt);
}
}
class任务扩展异步任务
{
@凌驾
受保护字符串doInBackground(无效…参数1){
字符串响应=null;
试一试{
HttpParams params=新的BasicHttpParams();
HttpConnectionParams.setSoTimeout(参数,0);
HttpClient HttpClient=新的默认HttpClient(参数);
字符串URL字符串=”http://url";
HttpGet HttpGet=新的HttpGet(urlString);
HttpEntity entity=httpClient.execute(httpget.getEntity();
response=EntityUtils.toString(实体);
httpClient.getConnectionManager().shutdown();
}
捕获(例外e)
{
e、 printStackTrace();
}
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
JSONArray JSONArray=新JSONArray(结果;
JSONObject jb=(JSONObject)jsonarray.getJSONObject(0);
字符串名称=jb.getString(“名称”);
字符串punkt=jb.getString(“punktezahl”);
fuehrender.setText(名称);
punkte.setText(punkt);
}
}
将try-catch块放在AsyncTask中。简短的回答是:不能在同一个线程上进行网络和ui。@user896692您需要使用thread或AsyncTask。AsyncTask更简单。可能是因为您正在调用connect()
来自UI
。从后台线程调用它
。但是我同意,这非常简单谢谢,这非常有用!但是我如何从onPostExecute访问我的TextView呢?@user896692将TextView声明为实例变量,并在onCreate中初始化它。将AsyncTask作为活动的内部类。如果AsyncTask是内部类,则ss,您可以在onPostExecute()方法中引用TextView。