Android httpclient.execute未给出结果也未出现异常
我在Android中有一个调用web服务的方法,如下所述 现在,当我的类调用这个方法时,我看不到任何异常,直到Android httpclient.execute未给出结果也未出现异常,android,web-services,Android,Web Services,我在Android中有一个调用web服务的方法,如下所述 现在,当我的类调用这个方法时,我看不到任何异常,直到System.out.println(“进入调用服务方法2”)日志。我可以在logcat看到 状态response=httpclient.execute(httppost)不工作且System.out.println(“进入呼叫服务方法3”)未在logcat上显示,也没有任何异常。 知道为什么吗?如何修复它 public void callService() thr
System.out.println(“进入调用服务方法2”)代码>日志。我可以在logcat看到
状态response=httpclient.execute(httppost)代码>不工作且System.out.println(“进入呼叫服务方法3”)代码>未在logcat上显示,也没有任何异常。
知道为什么吗?如何修复它
public void callService() throws Exception {
System.out.println("entered into call service method");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:81/a.php");
HttpResponse response;
System.out.println("entered into call service method 1");
try{
System.out.println("entered into call service method 2");
**response = httpclient.execute(httppost);**
System.out.println("entered into call service method 3");
是否在单独的线程/异步任务中调用此函数?如果应用程序中经常使用请求,我也建议考虑使用服务。
下面是一个POST方法,它对我很有用,但不要忘记以某种方式异步调用它:
public void executePost(String url, List<NameValuePair> postParams) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStream is = null;
try {
httppost.setEntity(new UrlEncodedFormEntity(postParams));
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
is = entity.getContent();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int inChar;
while ((inChar = is.read()) != -1) {
bos.write(inChar);
}
String resp = bos.toString();
// report back the resp e.g. via LocalBroadcast message
} else {
// report back e.g. via LocalBroadcast message
}
}
else {
// report back e.g. via LocalBroadcast message
}
} catch (Exception e) {
// report back the exception e.g. via LocalBroadcast message
// exception message: e.getMessage()
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void executePost(字符串url,列表后参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
InputStream=null;
试一试{
setEntity(新的UrlEncodedFormEntity(后参数));
HttpResponse response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==HttpStatus.SC\u OK){
HttpEntity=response.getEntity();
如果(实体!=null){
is=entity.getContent();
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
英查;
而((inChar=is.read())!=-1){
bos.write(英寸);
}
String resp=bos.toString();
//通过本地广播消息等方式报告响应
}否则{
//例如通过本地广播消息进行报告
}
}
否则{
//例如通过本地广播消息进行报告
}
}捕获(例外e){
//通过本地广播消息等方式报告异常
//异常消息:e.getMessage()
}最后{
如果(is!=null){
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
您能发布callService方法的整个源代码吗?谢谢各位,它工作正常。如果使用emulator,错误是使用10.0.2.2而不是localhost。如果使用emulator,错误是使用10.0.2.2而不是localhost