How To Use ProgressDialog To Show JSON Parsing Progress?
Solution 1:
Without any logcat it's difficult to help, but it seems that your ctx
is null so
dialog = new ProgressDialog(ctx);
the dialog can't be created.
Try to add constructor to the AsyncTask
and pass the context here, something like:
private class ParseTask extends AsyncTask<Void, Void, String> {
...
public ParseTask(Context ctx) {
this.ctx = ctx;
}
...
}
To start the task:
new ParseTask(this).execute();
Solution 2:
Well, I suppose this can be done in much easier and modern way.
- use GSON to parse Your JSON
- consider using Retrofit for REST
Now step by step:
add dependencies to Your gradle file:
compile "com.squareup.retrofit2:retrofit:$retrofitVersion" compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
this will let U use retrofit lib 2. Create retrofit serverAPI interface
public interface InternalServerAPI {
@GET("users/statistics")
Call<Example> healthcheckEndPoint(Params... params);
}
Create a corresponding to Your JSON object (POJO). U can use this online http://www.jsonschema2pojo.org. For instance U`ve got JSON like this:
{ "date":"1234343555", "widgets": [ { "title":"title1", "desc":"desc1" }, { "title":"title2", "desc":"desc2" }, ... ]
}
U`ll obtain two model classes like this:
public class Example {
@SerializedName("date")
@Expose
private String date;
@SerializedName("widgets")
@Expose
private List<Widget> widgets = new ArrayList<Widget>();
/**
*
* @return
* The date
*/
public String getDate() {
return date;
}
/**
*
* @param date
* The date
*/
public void setDate(String date) {
this.date = date;
}
/**
*
* @return
* The widgets
*/
public List<Widget> getWidgets() {
return widgets;
}
/**
*
* @param widgets
* The widgets
*/
public void setWidgets(List<Widget> widgets) {
this.widgets = widgets;
}
}
-----------------------------------com.example.Widget.java-----------------------------------
package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Widget {
@SerializedName("title")
@Expose
private String title;
@SerializedName("desc")
@Expose
private String desc;
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The desc
*/
public String getDesc() {
return desc;
}
/**
*
* @param desc
* The desc
*/
public void setDesc(String desc) {
this.desc = desc;
}
}
Now build the retrofit object:
Retrofit mRetrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .baseUrl(Constants.BASE_INTERNAL_SERVER_ADDRESS) .build();
And refer to correspondin endPoint, but first call your progress dialog:
dialog = new ProgressDialog(Parser.this); dialog.setMessage("Pasring..."); dialog.setIndeterminate(true); dialog.setCancelable(true); dialog.show(); Call<Example> fetchWidgets = mRetrofit.create(InternalServerAPI.class).healthcheckEndPoint(Params); fetchWidgets.enqueue(new Callback<Example>() { @Override public void onResponse(Call<Example> call, Response<Example> response) { //here response is your model object and U can refer to its fields ArrayList<Widget> data = response.getWidgets(); ... //update adapter ... //and now U can dismiss your dialog dialog.dismiss(); } @Override public void onFailure(Call<Example> call, Throwable t) { //here U can handle connection errors //and also dismiss dialog dialog.dismiss(); } });
Of course all of this should be done in some MVP way, but it is not a subject now.
Post a Comment for "How To Use ProgressDialog To Show JSON Parsing Progress?"