-1

I'm trying to get data by calling a web service, and for that I'm using an Async task class.

I want to use an “animated circle” while loading stuff (complete async task process). It will be really helpful if anyone can give me some idea how to do it.

I was looking at this answer by DBragion in Animated loading image in picasso, but doesn't explain exactly where to use those in the project.

Activity.java

       new AddressAsyncTask(getBaseContext(), new OnTaskCompletedObject() {

            @Override
            public void onTaskCompletedObject(JSONObject responseJson) {

                Constants.dataAddress = responseJson.toString();

                loaddata();

            }
        }).execute(email);

OnTaskCompletedObject.java

public interface OnTaskCompletedObject {
    void onTaskCompletedObject(JSONObject responseJson);
}

AddressAsyncTask.java

public class AddressAsyncTask extends AsyncTask<String, Integer, JSONObject> {

    private OnTaskCompletedObject listener;
    private JSONObject responseJson = null;
    private Context contxt;
    private Activity activity;
    String email;

    public AddressAsyncTask(Context context, OnTaskCompletedObject onTaskCompletedObject) {

        this.contxt = context;
        this.listener=onTaskCompletedObject;
    }

    // async task to accept string array from context array
    @Override
    protected JSONObject doInBackground(String... params) {

        String path = null;
        String response = null;
        HashMap<String, String> request = null;
        JSONObject requestJson = null;
        DefaultHttpClient httpClient = null;
        HttpPost httpPost = null;
        StringEntity requestString = null;
        ResponseHandler<String> responseHandler = null;

        // get the email and password
        Log.i("Email", params[0]);

        try {

            path = "http://xxxxxxxxxxxxxxxxx/MemberDetails";
            new URL(path);
        } catch (MalformedURLException e) {

            e.printStackTrace();
        }

        try {

            // set the API request
            request = new HashMap<String, String>();
            request.put(new String("Email"), params[0]);
            request.entrySet().iterator();

            // Store locations in JSON
            requestJson = new JSONObject(request);
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(path);
            requestString = new StringEntity(requestJson.toString());

            // sets the post request as the resulting string
            httpPost.setEntity(requestString);
            httpPost.setHeader("Content-type", "application/json");

            // Handles the response
            responseHandler = new BasicResponseHandler();
            response = httpClient.execute(httpPost, responseHandler);

            responseJson = new JSONObject(response);

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        try {
            responseJson = new JSONObject(response);

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return responseJson;

    }

    @Override
    protected void onPostExecute(JSONObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        listener.onTaskCompletedObject(responseJson);
    }

}
Community
  • 1
  • 1
modabeckham
  • 227
  • 5
  • 19
  • And what exactly is the problem? You can't just come here explain what you want to happen and then wait for someone here to implement it for you. – Xaver Kapeller Apr 17 '15 at 05:18
  • use this library https://github.com/lzyzsd/CircleProgress – Viks Apr 17 '15 at 05:18
  • As a first step explain what exactly is the problem,what you have tried, what kind of errors got encountered. lf you cannot write a question which contains a clear problem statement and not just "how do you do x" then you either have not tried enough or the question is just not a good fit for Stack Overflow. Before you don't explain exactly what you are having trouble with I can only guess what the solution might be. – Xaver Kapeller Apr 17 '15 at 05:36
  • 1
    u can try the answer of Kurru in http://stackoverflow.com/questions/5442183/using-the-animated-circle-in-an-imageview-while-loading-stuff – John David Apr 17 '15 at 05:53
  • @Xaver Kapeller thanks for your advice, I'll try to post proper questions – modabeckham Apr 17 '15 at 05:58

1 Answers1

0

Show Progress Dialog in the onpreExecute method of AsyncTask and dismiss the dialog in Post Execute.I hope that gets your problem solved

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34