OnPostExecute结果为null

时间:2015-07-17 11:47:33

标签: java android

我正在从我的应用程序向外部服务器发送一些数据。

public class PostKey extends AsyncTask<String, String, String> {
    public AsyncResponse delegate = null;

    @Override
    protected String doInBackground(String... params) {
        postData(params[0]);
        return null;
    }

    @Override
    protected void onPostExecute(String result){
        Log.d(String.valueOf(result), " Result?");
        delegate.processFinish(result); // Returns NullPointerException
    }

    public void postData(String valueIWantToSend) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost     = new HttpPost("http://domain.com/post.php");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("hoi", "test"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());

            Log.d(responseStr, "");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

    public interface AsyncResponse {
        void processFinish(String output);
    }
}

Log.d(responseStr, "");确实返回true或false,具体取决于发送的数据,并记录它。

delegate.processFinish(result);返回NullPointerException,同时提及public class PostKey extends AsyncTask<String, String, String>,所有参数都声明为Strings

我也尝试将result替换为responseStr,但这也不起作用。

3 个答案:

答案 0 :(得分:1)

方法调用问题delegate.processFinish(result); 委托未初始化。所以你在null对象上调用一个方法。

答案 1 :(得分:0)

将您的代码更改为

public class PostKey extends AsyncTask<String, String, String> {
    public AsyncResponse delegate = null;

    public PostKey(AsyncResponse asyncResponse) {
        delegate = asyncResponse;
    }   

    @Override 
    protected String doInBackground(String... params) {
        return postData(params[0]);
    } 

    @Override 
    protected void onPostExecute(String result){
        Log.d(String.valueOf(result), " Result?");
        delegate.processFinish(result);
    } 

    public String postData(String valueIWantToSend) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost     = new HttpPost("http://domain.com/post.php");

        try { 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("hoi", "test"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());

            Log.d(responseStr, "");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block 
        } catch (IOException e) {
            // TODO Auto-generated catch block 
        } 
        return responseStr;
    } 

    public interface AsyncResponse { 
        void processFinish(String output);
    } 
} 

答案 2 :(得分:0)

如果你想得到postData()的结果,那么将postData()的返回类型从void更改为String,并将结果返回给onPostExecute,如下所示:

public class PostKey extends AsyncTask<String, String, String> {
    public AsyncResponse delegate = null;

    @Override
    protected String doInBackground(String... params) {
        return postData(params[0]);
    }

    @Override
    protected void onPostExecute(String result){
        Log.d(String.valueOf(result), " Result?");
        delegate.processFinish(result); // Returns NullPointerException
    }

    public String postData(String valueIWantToSend) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost     = new HttpPost("http://domain.com/post.php");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("hoi", "test"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());
            return responseStr;
            Log.d(responseStr, "");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
       return null;
    }

    public interface AsyncResponse {
        void processFinish(String output);
    }
}