【发布时间】:2012-02-06 14:14:39
【问题描述】:
我不明白,为什么我无法从任何 url 获得没有错误的 http 响应。
package de.vogella.android.asynctask;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
public class SimpleWebGrab extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grabURL("http://android.com");
}
public void grabURL(String url) {
//new GrabURL().execute(url);
GrabURL grabURL = new GrabURL(); // Создаем экземпляр
grabURL.execute(url); // запускаем
}
private class GrabURL extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(SimpleWebGrab.this);
protected void onPreExecute() {
Dialog.setMessage("Загрузка данных..");
Dialog.show();
}
protected Void doInBackground(String... urls) {
try {
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
if (Error != null) {
Toast.makeText(SimpleWebGrab.this, Error, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SimpleWebGrab.this, "Источник: " + Content, Toast.LENGTH_LONG).show();
}
}
}
}
我在这行得到错误:
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
错误文本是:
Connection to http://android.com refused
我使用哪个网址都没有关系。全部都一样。在 Step Into 之后的这一行上,我在调试时收到 Class file editor: "source not found" 消息,但如果我按下 Run,应用程序不会崩溃:
HttpGet httpget = new HttpGet(urls[0]);
是连接被拒绝的原因吗?如果是,如何解决?提前致谢。
【问题讨论】:
-
mb 我应该放这样的东西吗?设置。 System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "192.168.1.1:8080");虽然它不起作用
-
我建议您在具有正常连接的设备上尝试它,以查看它是代理,还是在不同的 PC 上。您也可以使用 EasyHttpClient 库来查看它是否适合您github.com/uscheller/EasyHttpClient