0

我有一个实现监听器的类,这样当另一个具有异步任务的类完成时,可以使用这个监听器返回数据。异步任务正在完成,但在后执行函数中获取数据时,侦听器未传递数据。

异步任务

Public class AdFetcher extends AsyncTask<String,Void,String>
{
private HTTPListener listener;

//sets the listeners
public AdFetcher(HTTPListener listener)
{
   this.listener = listener;
}

@Override
protected String doInBackground(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;
    try {
        response = httpclient.execute(new HttpGet(uri[0]));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        //TODO Handle problems..
    } catch (IOException e) {
        //TODO Handle problems..
    }
    return responseString;
}

@Override
protected void onPostExecute(String result)
{
    //Tries to deserialization and if there is an exception then its added to the Stack Trace.
    try
    {
        JSONObject data = new JSONObject(result);
        if(listener==null)
            throw new NullPointerException();
        //Calls onComplete function in the class/Activity that implemented the listener.
        listener.onComplete(data);
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }



}

}

听众

public interface HTTPListener{

/**
 * Callback function that must be implemented
 * to get result from HTTP worker thread.
 * @param result
 */
public void  onComplete (JSONObject result);

}//end HttpListener interface

类实现监听器

public class AdManager implements HTTPListener
{
//will store the current context of the application.
private  Context context;
private Utility utils;
private String url;
private WebView client;
public AdManager(Context context,WebView client)
{
   this.client = client;
   this.context = context;
}

public void getAd()
{
    utils = new Utility();
    url =   utils.BuildUrl();
    new AdFetcher(this).execute(url);
}


public void  onComplete(JSONObject result)
{     
     try
     {   Log.e("RESULTS",result.getString("adtype"));
         if(result.getString("error") == "null")
         {

             if(result.getString("adtype") == "banner")
             {   //loads the banner image in the webview.

                 String html = "<a href=\""+result.getString("adclick_url")+"\"><img src=\""+result.getString("adimage")+"\"></a>";
                 String mime = "text/html";
                 String encoding = "utf-8";
                 client.setVisibility(View.VISIBLE);
                 client.getSettings().setJavaScriptEnabled(true);
                 client.loadDataWithBaseURL(null, html, mime, encoding, null);


             }

         }

     }
     catch (JSONException e)
     {
         e.printStackTrace();
     }

}
}
4

0 回答 0