我正在尝试从网站获取数据并将其解析到我的 android 应用程序中。不幸的是,我什至没有进入解析数据的部分。在以下行之后代码不会运行:
HttpResponse response = httpclient.execute(httpget);
结果是 message = 333。当我使用 eclipse 调试器跳过它时,我可以看到它运行了该行。然后我可以读出响应变量(= 200),但之后的行不执行。它没有到达函数中的 444 或其他代码。我已经在 Android 清单中声明了互联网权限。
编码:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (TextView)findViewById(R.id.message);
message.setText("111");
//new getinternetData .execute("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onStart(){
super.onStart();
new getinternetData().execute("");
//message.setText("OnStart");
}
private class getinternetData extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.com");
try{
message.setText("333");
HttpResponse response = httpclient.execute(httpget);
message.setText("444");
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200){
message.setText("Could connect");
}else
message.setText("Couldn't connect");
}catch(Exception e){
message.setText(e.toString());
}
message.setText("555");
return null;
}
@Override
protected void onPostExecute(String result) {
//message.setText("Postsequence");
}
}
我不知道我做错了什么。
是因为我没有正确使用 AsyncTask 吗?
或者可能没有正确使用 httpclient.execute?
编辑:感谢您的帮助,我已将消息更改为 log.d,现在我可以看到我可以连接。故障在于更新屏幕等。我有很多东西要学。
这是我更改的代码部分:
private static final String TAG = "MainActivity";
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.com");
try{
//message.setText("333");
Log.d(TAG, "Before httpResponse");
HttpResponse response = httpclient.execute(httpget);
Log.d(TAG, "After httpResponse");
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
Log.d(TAG, "after statusCode, code= " +statusCode);
if(statusCode == 200){
Log.d(TAG, "Could connect");
}else{
Log.d(TAG, "Couldn't connect");
}
}catch(Exception e){
Log.d(TAG, e.toString());
}
Log.d(TAG, "Exit do in background");
return null;
日志猫结果:
: D/MainActivity(22323): Before httpResponse
bunch of code
: D/MainActivity(22323): After httpResponse
: D/MainActivity(22323): after statusCode, code= 200
: D/MainActivity(22323): Could connect
: D/MainActivity(22323): Exit do in background