我正在学习android。我开始研究与android的网络。我面临的问题是在try语句下httpclient.execute(request)行无法正常工作。而是每次都执行catch子句。我搜索了很长时间但我没有得到关于这个问题的答案。我很乐意从你这边得到任何帮助。
这是主要活动
package com.example.androidaspect;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
String url = "http://www.google.com";
TextView tv = (TextView) findViewById(R.id.tvres);
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute(url, context, tv);
}
}
这是Asynctaskrunner
package com.example.androidaspect;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;
public class AsyncTaskRunner extends AsyncTask<String, String, String>{
Context context;
TextView tv;
String url;
String result="";
public void execute(String url, Context context, TextView tv) {
// TODO Auto-generated method stub
this.tv= tv;
this.context = context;
this.url = url;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
publishProgress("Loading");
HttpClient hc = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse res = hc.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
String line="";
while((line=rd.readLine())!=null){
result.concat(line);
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tv.setText(result);
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
Toast.makeText(context, values[0], Toast.LENGTH_SHORT).show();
}
}
我已添加了互联网权限。我的xml中有一个textview,它从网站获得响应。但是当我在模拟器上运行应用程序时,它会运行,但textview不会加载数据。
答案 0 :(得分:2)
正如我在评论中提到的,使用AsyncTask。
引用文档
应用程序尝试执行时抛出的异常 在其主线程上进行网络操作。
这仅适用于针对Honeycomb SDK或。的应用程序 更高。允许使用早期SDK版本的应用程序 他们的主要事件循环线程上的网络,但它很重要 泄气
http://developer.android.com/reference/android/os/AsyncTask.html
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tvres);
tv = (TextView) findViewById(R.id.textView1);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... arg0) {
// TODO Auto-generated method stub
String response=null;
try {
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet("http://www.google.com");
HttpResponse res = hc.execute(hg);
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
tv.setText(result);
}
}
你有AsyncTask作为内部类。
class TheTask extends AsyncTask<Void,Void,String>
你没有将任何内容传递给doInbackground
。所以第一个arg类型是Void。第二个arg也是Void因为你没有进度指示器。第三个是背景计算结果的类型。所以它的字符串是因为我们return response;
是字符串。
现在在后台线程上调用doInbackground
。所以在那里进行背景计算。
onPostExecute
。所以在那里更新ui。
编辑:
你的方式
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.google.com";
tv = (TextView) findViewById(R.id.tvres);
AsyncTaskRunner runner = new AsyncTaskRunner(url,this,tv);
runner.execute();
}
}
然后
public class AsyncTaskRunner extends AsyncTask<Void, String, String>{
Context context;
TextView tv;
String url;
String result="";
public AsyncTaskRunner(String url, Context context, TextView tv) {
// TODO Auto-generated method stub
this.tv= tv;
this.context = context;
this.url = url;
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
publishProgress("Loading");
String response=null;
try {
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet(url);
HttpResponse res = hc.execute(hg);
HttpEntity entity = res.getEntity();
response = EntityUtils.toString(entity);
}catch(Exception e){
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tv.setText(result);
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
Toast.makeText(context, values[0], Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:0)
您需要使用AsyncTask&lt;&gt;或IntentService来处理HttpQueries