我尝试通过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:连接到192.168.56.1:8080被拒绝。 洛奇给我这个文章:
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中很新,所以你能给我一些帮助吗?
答案 0 :(得分:0)
您确定自己的应用正在提出相应的要求吗?您应该使用方法HTTPResponse
和getStatusLine()
检查getStatusCode()
中返回的状态代码。如果您的请求是正确的,您应该从getStatusCode()
(HttpStatus.SC_OK
)返回200。否则,请查看您的应用正在发出的请求。
你可以使用像netcat这样的工具来查看你的应用程序在其GET方法中发送的确切内容,以便在PC上的端口上进行侦听并让应用程序连接到该端口。
如果一切看起来没问题请尝试自己手动发送(你可以使用netcat或者有一个很好的RESTClient调试器插件用于Firefox)
如果这表明您正在获取数据,那么我怀疑您的问题在于您的缓冲读卡器。您可以使用Log.d()
语句打印在代码中各个点显示字符串值的logcat语句,以了解事情的进展情况
答案 1 :(得分:0)
我想以下步骤对您有所帮助。
如果在清单中授予互联网的权限,那么问题是您可能不在主机(192.168.56.1:8080)所在的网络中......所以尝试连接相同的网络...例如:我假设是一个私人wifi,所以连接到该网络并关闭数据包数据。所以现在你可以连接它了。
或者,如果您想通过其他网络连接到它,那么您必须继续使用该主机(192.168.56.1:8080)并要求您的网络管理员将其设为公共网址,以便您可以从任何地方点击它。
我希望它可以帮到你。