2012-08-24 113 views
0

当我的用户点击登录按钮时,我想打我的web服务。我有下面的代码来做到这一点。未被调用:onPostExecute

public void onClick(final View view) { 
    String orgKey = inputCompany.getText().toString(); 
    new getAppInfo().execute("http://example.webservice.com"); 

这是我getAppInfo

private class getAppInfo extends AsyncTask<String, Void, String> { 
    /** The system calls this to perform work in a worker thread and 
     * delivers it the parameters given to AsyncTask.execute() */ 
    @Override 
    protected String doInBackground(String... urls) { 
     String xml = null; 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(urls[0]); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      xml = EntityUtils.toString(httpEntity); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // return XML 
     return xml; 
    } 

    /** The system calls this to perform work in the UI thread and delivers 
     * the result from doInBackground() */ 
    @Override 
    protected void onPostExecute(String result) { 
     Document doc = GetDomElement(result); // getting DOM element 

     NodeList nl = doc.getElementsByTagName("details"); 
     getChildElements(nl); 
    } 

doBackground正在运行,但onPostExecute不是。我已经将它从点击中移出,它已经工作,但我需要它在onClick中。

我如何获得它在我的onClick中运行onPostExecute

回答

0

语法正确,您可能会在后台进程中收到异常,并停止后台线程。在doInBackground的末尾放置一个日志语句或者添加一个catch(Throwable t)给你的try。

有信心 - 只要doInBackground成功完成,它就会被调用。另外,你应该在后台做你的DOM解析 - 目前你正在UI线程中做这些,这可能会导致ANR弹出窗口。

+0

如果这个选项很大,你可能也会得到一个OutOfMemoryError - 你也可以在Android中捕获它(与Java不同) – siliconeagle

+0

另外,请确保你的清单中有Internet权限 – siliconeagle

+0

感谢您的建议 – BigT

0

onPostExecute总是被调用一次doInBackground完成。尝试记录东西在onPostExecute以确认此行为。


doInBackground方法中添加一个一般catch声明。

catch(Exception e){ 
    e.printStackTrace(); 
} 
+0

doInBackground已完成。它仍然不叫 – BigT

+0

尝试记录onPostExecute中的东西,并看到它的工作原理 – waqaslam

+0

它甚至没有进入onPostExecute。这是问题。它做doInBackground,但不是onPostExecute。 – BigT

0

首先,一个普通的指针 - 类名在Java中通常是大写的CamelCase,方法名是小写的camelCase。

在你的问题 - 你是否有机会退出活动或关闭产生任务的对话框?如果它在onclick处理程序之外工作,我的猜测是有些东西正在销毁AsyncTask试图执行该方法的Handler对象。

尝试在当前Activity的Handler上发布一个Runnable(它执行AsyncTask)。