问题描述
目前,我有一个需要执行的AsyncTask的HTTP POST多个活动,我想提出的AsyncTask作为另一个类文件,以使不同的活动可以调用AsyncTask的执行HTTP POST请求和onPostExecute,调用该方法HTT在调用该AsyncTask的活动presult(结果)。我试图传递活动,但我无法调用onPostExecute的方法。我该怎么办呢?
公共类MyActivity延伸活动{
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_MyActivity);
//逻辑在这里...
AsyncHttpPost asyncHttpPost =新AsyncHttpPost(somecontent写入,这一点,对话);
JSONObject的数据=新的JSONObject();
data.put(钥匙,价值);
尝试 {
asyncHttpPost.execute(数据);
}
赶上(例外前){
ex.printStackTrace();
}
}
公共静态无效HTT presult(字符串结果){
//此方法得到调用的AsyncTask来进行更改UI
}
}
AsyncHttpPost.java
公共类AsyncHttpPost扩展的AsyncTask< JSONObject的,字符串,字符串> {
字符串recvdjson;
字符串MDATA =;
私人ProgressDialog对话框;
私人活动的活动;
公共AsyncHttpPost(字符串数据,活动活动,ProgressDialog对话框){
MDATA =数据;
this.activity =活动;
this.dialog =对话框;
}
在preExecute保护无效()
{
dialog.show();
}
@覆盖
保护字符串doInBackground(JSONObject的... PARAMS){
//逻辑做的http请求
返回someStringResult;
}
保护无效onPostExecute(字符串结果){
dialog.dismiss();
activity.htt presult(结果); //此行给我的错误:该方法HTT presult(字符串)是未定义的类型活动
}
}
在一个单独的文件创建一个名为Htt的presponseImpl接口,并添加所需的方法 HTT presult
接口的Htt presponseImpl {
公共无效HTT presult(字符串结果);
}
现在实现这个接口由您活动
类
公共类MyActivity扩展活动实现的Htt presponseImpl {
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_MyActivity);
//逻辑在这里...
AsyncHttpPost asyncHttpPost =新AsyncHttpPost(somecontent写入,这一点,对话);
JSONObject的数据=新的JSONObject();
data.put(钥匙,价值);
尝试 {
asyncHttpPost.execute(数据);
}
赶上(例外前){
ex.printStackTrace();
}
}
公共无效HTT presult(字符串结果){
//此方法得到调用的AsyncTask来进行更改UI
}
}
和你AsyncHttpPost类为。
公共类AsyncHttpPost扩展的AsyncTask< JSONObject的,字符串,字符串> {
字符串recvdjson;
字符串MDATA =;
私人ProgressDialog对话框;
私人的Htt presponseImpl HTT presponseImpl;
公共AsyncHttpPost(字符串数据,Htt的presponseImpl HTT presponseImpl,ProgressDialog对话框){
MDATA =数据;
this.htt presponseImpl = HTT presponseImpl;
this.dialog =对话框;
}
在preExecute保护无效()
{
dialog.show();
}
@覆盖
保护字符串doInBackground(JSONObject的... PARAMS){
//逻辑做的http请求
返回someStringResult;
}
保护无效onPostExecute(字符串结果){
dialog.dismiss();
HTT presponseImpl.htt presult(结果);
}
}
实现这个的Htt presponseImpl
界面,从中你想要做的Htt $你活动
类p $ pquest。
I currently have multiple activity that needs to perform an asynctask for http post and I wish to make the asynctask as another class file so that the different activity can call the asynctask to perform the http post request and onPostExecute, call the method httpResult(result) in the activity that called the asynctask. I have tried to pass the activity in but I am unable to call the method in onPostExecute. How can I do that?
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MyActivity);
//logic here...
AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
JSONObject data = new JSONObject();
data.put("key", "value");
try {
asyncHttpPost.execute(data);
}
catch (Exception ex){
ex.printStackTrace();
}
}
public static void httpResult(String result) {
//this method has to get called by asynctask to make changes to the UI
}
}
AsyncHttpPost.java
public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
String recvdjson;
String mData="";
private ProgressDialog dialog;
private Activity activity;
public AsyncHttpPost(String data, Activity activity, ProgressDialog dialog) {
mData = data;
this.activity = activity;
this.dialog = dialog;
}
protected void onPreExecute()
{
dialog.show();
}
@Override
protected String doInBackground(JSONObject... params) {
//logic to do http request
return "someStringResult";
}
protected void onPostExecute(String result) {
dialog.dismiss();
activity.httpResult(result); //This line gives me error : The method httpResult(String) is undefined for the type Activity
}
}
Create an Interface called HttpResponseImpl in a seperate file and add the required method httpResult
interface HttpResponseImpl{
public void httpResult(String result);
}
Now implement this interface by you Activity
class
public class MyActivity extends Activity implements HttpResponseImpl{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MyActivity);
//logic here...
AsyncHttpPost asyncHttpPost = new AsyncHttpPost("someContent", this, dialog);
JSONObject data = new JSONObject();
data.put("key", "value");
try {
asyncHttpPost.execute(data);
}
catch (Exception ex){
ex.printStackTrace();
}
}
public void httpResult(String result){
//this method has to get called by asynctask to make changes to the UI
}
}
And your AsyncHttpPost class would be.
public class AsyncHttpPost extends AsyncTask<JSONObject, String, String> {
String recvdjson;
String mData="";
private ProgressDialog dialog;
private HttpResponseImpl httpResponseImpl;
public AsyncHttpPost(String data, HttpResponseImpl httpResponseImpl, ProgressDialog dialog) {
mData = data;
this.httpResponseImpl = httpResponseImpl;
this.dialog = dialog;
}
protected void onPreExecute()
{
dialog.show();
}
@Override
protected String doInBackground(JSONObject... params) {
//logic to do http request
return "someStringResult";
}
protected void onPostExecute(String result) {
dialog.dismiss();
httpResponseImpl.httpResult(result);
}
}
Implements this HttpResponseImpl
interface with all you Activity
class from which you want to do HttpRequest.
这篇关于安卓通用的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!