2013-01-19 23 views
0

在我的android應用程序中,我已經從HTML頁面調用一個JavaScript方法到java頁面。如何在JavaScript中創建異步任務

this.start = function() 
{  
    try 
    { 
      Mynamespace.myapi.getvalue("myvalue", { 
      onSuccess: function (data) 
      { 
       value= JSON.parse(data); 
       alert("called"); 
      }, 
      onFailure: function (error) { } 
      }); 
     } 
     catch (err) { } 
     if (value== null) { value= new Object(); 
    }; 

在上文中,的GetValue方法被稱爲我的Java類和返回一些的值,但得到的onSuccess/onFailure處內的值之前,下面的線被調用。 How to wait the method up to the callback is called?

if (value== null) { value= new Object(); 

回答

0

我分析更多關於我的查詢。最後我得到了解決方案。 String that i have loaded with webview was having more single quotes so unable to load with webview。因此,不是調用成功失敗回調函數,而是調用下一行。

現在我用我的java類中的「\'」替換了單引號「\'」。工作正常。 :-)

0
private class LongOperation extends AsyncTask<String, Void, String> 
{ 
    protected void onPreExecute() 
    { 
     progressDialog = new ProgressDialog(activity.this); 
     progressDialog.setTitle("Processing..."); 
     progressDialog.setMessage("Please wait..."); 
     progressDialog.setCancelable(true); 
     progressDialog.show(); 
    } 

    protected String doInBackground(String... params) 
    { 
     try 
     { 
      //Getting data from server 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    protected void onPostExecute(String result) 
    { 
     progressDialog.dismiss(); 
    } 
    } 

如何調用該

ProgressDialog progressDialog; 
LongOperation mytask = null; 
mytask = new LongOperation(); 
mytask.execute(); 
+0

謝謝。但我想在JavaScript中的異步任務 – Ponmalar