我正在尝试让一个简单的网络阅读器在 AsyncTast 中工作,
主线程
public class main extends Activity implements CallBackListener{
...
WebReader wr = new WebReader();
wr.execute("http://www.google.com");
...
@Override
public void callback(String res)
{
Log.e("eee",res);
}
...
}
...
interface CallBackListener
{
void callback(String res);
}
和网络阅读器
class WebReader extends AsyncTask<String, Void, String>{
CallBackListener mListener;
public void setListener(CallBackListener listener){
mListener = listener;
}
@Override
protected String doInBackground(String... urls) {
String response = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
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) {
Log.e("s3","ss"+result);
mListener.callback(result); // null point here
}
}
然而它在 mListener.callback 处因空点而崩溃......任何想法可能有什么问题?
谢谢!