我尝试通过 AsyncTask 从 Web 服务获取一些数据,但它返回一个空响应并且需要很长时间才能连接。不想在这里放很多代码,但我认为这是解释我所做的更好的方式。这是我实现连接的类。
public class WebService extends AsyncTask<String, String, String> {
private ArrayList<SimpleObserver> listeners;
private int responseCode;
private String message;
private String response;
public WebService() {
listeners = new ArrayList<SimpleObserver>();
}
public void addListener(SimpleObserver obs) {
listeners.add(obs);
}
public void removeListener(SimpleObserver obs) {
listeners.remove(obs);
}
public void notifyListener(String s) {
for (SimpleObserver listener : listeners)
listener.onChange(s);
}
public String getResponse() {
return response;
}
public String getErrorMessage() {
return message;
}
public int getResponseCode() {
return responseCode;
}
@Override
protected void onPreExecute() {
notifyListener("Calculating..");
}
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
notifyListener("Execute");
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
notifyListener("Chegou aqui");
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
notifyListener(result);
}
}
并打电话给我:
public class ServiceController implements SimpleObserver {
private Activity act;
private WebService s;
private static final String URL = "my url with parameters";
public ServiceController(MainActivity mainActivity) {
act = mainActivity;
s = new WebService();
s.addListener(this);
}
public void onChange(String s) {
((MainActivity) act).getTextView().setText(s);
}
public void executeService() {
try {
s.execute(new String[] { URL });
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
我正在测试我的应用程序,它给了我这个 HttpHostConnectException:Connection to 192.168.56.1:8080 denied 。Locat给我这个消息:
08-24 16:58:02.060: W/System.err(17595): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.56.1:8080 refused
08-24 16:58:02.075: W/System.err(17595): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
08-24 16:58:02.075: W/System.err(17595): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-24 16:58:02.075: W/System.err(17595): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-24 16:58:02.075: W/System.err(17595): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-24 16:58:02.075: W/System.err(17595): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-24 16:58:02.075: W/System.err(17595): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-24 16:58:02.075: W/System.err(17595): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-24 16:58:02.075: W/System.err(17595): at com.example.pulldata.DS.WebService.doInBackground(WebService.java:68)
08-24 16:58:02.075: W/System.err(17595): at com.example.pulldata.DS.WebService.doInBackground(WebService.java:1)
08-24 16:58:02.080: W/System.err(17595): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-24 16:58:02.080: W/System.err(17595): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-24 16:58:02.080: W/System.err(17595): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-24 16:58:02.080: W/System.err(17595): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-24 16:58:02.080: W/System.err(17595): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-24 16:58:02.080: W/System.err(17595): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-24 16:58:02.085: W/System.err(17595): at java.lang.Thread.run(Thread.java:856)
08-24 16:58:02.085: W/System.err(17595): Caused by: java.net.ConnectException: failed to connect to /192.168.56.1 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
08-24 16:58:02.085: W/System.err(17595): at libcore.io.IoBridge.connect(IoBridge.java:114)
08-24 16:58:02.085: W/System.err(17595): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-24 16:58:02.085: W/System.err(17595): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-24 16:58:02.085: W/System.err(17595): at java.net.Socket.connect(Socket.java:842)
08-24 16:58:02.085: W/System.err(17595): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-24 16:58:02.085: W/System.err(17595): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-24 16:58:02.090: W/System.err(17595): ... 15 more
08-24 16:58:02.090: W/System.err(17595): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
08-24 16:58:02.090: W/System.err(17595): at libcore.io.Posix.connect(Native Method)
08-24 16:58:02.090: W/System.err(17595): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
08-24 16:58:02.090: W/System.err(17595): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-24 16:58:02.090: W/System.err(17595): at libcore.io.IoBridge.connect(IoBridge.java:112)
08-24 16:58:02.095: W/System.err(17595): ... 20 more
我搜索了这个问题的其他问题,他们都说这是由于缺乏互联网许可,但我的清单中有这个!我完全不明白!
我是android的新手,你能给我一些帮助吗?