Issue
I wrote a class that extend async task; i send data to it and class return a json object
But on pre-execute function on async task not working and my program go in not responding mode until class finish its job
public class async_Task extends AsyncTask<Void, Void, JSONObject>
{
Context mycontext;
List<NameValuePair> query_List=new ArrayList<NameValuePair>();
String str_URL;
ProgressDialog progressDialog;
JSONObject jsonObject =null;
JSONParser jsonParser =new JSONParser();
public async_Task(Context context,List<NameValuePair> lst_NameValuePairs,String url_String)
{
this.mycontext =context;
this.query_List =lst_NameValuePairs;
this.str_URL =url_String;
progressDialog =new ProgressDialog(mycontext, ProgressDialog.THEME_HOLO_LIGHT);
}
@Override
protected void onPreExecute()
{
progressDialog.setMessage("wait please...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected JSONObject doInBackground(Void... arg0)
{
try
{
jsonObject=jsonParser.store_And_Feedback(str_URL, query_List);
}
catch (Exception e)
{
try
{
jsonObject.put("res_code", "-5");
jsonObject.put("res", e.getMessage());
}
catch (JSONException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject args)
{
progressDialog.dismiss();
}
}
and i called async task in my activity
jsonObject=new async_Task(JAct_Singup.this, lst_NameValuePairs, str_Url).execute().get();
i need to get result(jsonobject) form async task and when remove get(); i give this error
Type mismatch: cannot convert from AsyncTask<Void,Void,JSONObject> to JSONObject
Solution
finally i found solutions
1- create new interface
class
public interface AsyncResponse
{
void progressEnd(JSONObject jsonObject);
}
2- declare class in async task class
public class async_Task extends AsyncTask<Void, Void, JSONObject>
{
public AsyncResponse response =null;
@Override
protected void onPreExecute()
{
....
}
@Override
protected JSONObject doInBackground(Void... arg0)
{
...
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject args)
{
...
response.progressEnd(jsonObject);
}
}
3- implements class or activity from created interface
class(AsyncResponse)
public class JAct_Singup extends Activity implements AsyncResponse
{
.........
@Override
public void progressEnd(JSONObject jsonObject)
{
...
}
}
each time you call async task class the progressEnd
called after finish async task
Answered By - HamidTB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.