问题描述
我正在将一些数据从我的应用程序发送到外部服务器。
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
public class PostKey extends AsyncTask<String, String, String>
也提到public class PostKey extends AsyncTask<String, String, String>
。
我也尝试过用responseStr
替换result
,但这也不起作用。
1楼
您的方法调用的问题delegate.processFinish(result);
委托未初始化。
因此,您正在对空对象调用方法。
2楼
将您的代码更改为
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);
}
}
3楼
如果您想要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);
}
}