Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OnPostExecute结果为空_Java_Android - Fatal编程技术网

Java OnPostExecute结果为空

Java OnPostExecute结果为空,java,android,Java,Android,我正在将一些数据从我的应用程序发送到外部服务器 public class PostKey extends AsyncTask<String, String, String> { public AsyncResponse delegate = null; @Override protected String doInBackground(String... params) { postData(params[0]); retur

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

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);
    }
}
public类PostKey扩展异步任务{
公共异步响应委托=null;
@凌驾
受保护的字符串doInBackground(字符串…参数){
postData(参数[0]);
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
Log.d(String.valueOf(result),“result?”);
delegate.processFinish(result);//返回NullPointerException
}
public void postData(字符串值IwantToSend){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://domain.com/post.php");
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“hoi”、“测试”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
字符串responsest=EntityUtils.toString(response.getEntity());
日志d(响应号“”);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
公共接口异步响应{
void processFinish(字符串输出);
}
}
Log.d(responsest,”)
根据发送的数据返回true或false,并记录它

delegate.processFinish(结果)
返回NullPointerException,其中还提到
公共类PostKey扩展异步任务
事件,尽管所有参数都声明为
字符串


我也尝试过用
responseStr
替换
result
,但也没用。

将代码改成那样

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);
    } 
} 
public类PostKey扩展异步任务{
公共异步响应委托=null;
公钥(异步响应异步响应){
委托=异步响应;
}   
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回postData(参数[0]);
} 
@凌驾
受保护的void onPostExecute(字符串结果){
Log.d(String.valueOf(result),“result?”);
委托。进程完成(结果);
} 
公共字符串postData(字符串值IwantToSend){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://domain.com/post.php");
试试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“hoi”、“测试”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
字符串responsest=EntityUtils.toString(response.getEntity());
日志d(响应号“”);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
} 
返回响应;
} 
公共接口异步响应{
void processFinish(字符串输出);
} 
} 

方法调用的问题
委托.processFinish(结果)

委托未初始化。因此,您正在对null对象调用一个方法。

如果需要postData()的结果,请将postData()的返回类型从void更改为String,并将该结果返回到onpostdecute,如下所示:

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);
    }
}
public类PostKey扩展异步任务{
公共异步响应委托=null;
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回postData(参数[0]);
}
@凌驾
受保护的void onPostExecute(字符串结果){
Log.d(String.valueOf(result),“result?”);
delegate.processFinish(result);//返回NullPointerException
}
公共字符串postData(字符串值IwantToSend){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://domain.com/post.php");
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“hoi”、“测试”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
字符串responsest=EntityUtils.toString(response.getEntity());
返回响应;
日志d(响应号“”);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
返回null;
}
公共接口异步响应{
void processFinish(字符串输出);
}
}

您的活动实现了接口?@Raghunandan是的,它不会从doInBackground方法返回null,因此在OnPostExecute中获得null您可以将其传递给asynctask的构造函数并执行
委托=(AsyncResponse)活动注意:活动实现接口。
void postData
=>
String postData
Log.d(responseStr,”
=>
返回响应estr
(也在该方法末尾添加
return null;
doInBackground的主体
=>
返回postData(参数[0])。。。添加空签入
onPostExecute
。。。如果未设置代理,则代理仍然可以为空。。。