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:
privateclassParseTaskextendsAsyncTask<Void, Void, String> {
...
publicParseTask(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
publicinterfaceInternalServerAPI {
@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:
publicclassExample {
@SerializedName("date")
@ExposeprivateString date;
@SerializedName("widgets")
@ExposeprivateList<Widget> widgets = newArrayList<Widget>();
/**
*
* @return
* The date
*/publicStringgetDate() {
return date;
}
/**
*
* @paramdate
* The date
*/publicvoidsetDate(String date) {
this.date = date;
}
/**
*
* @return
* The widgets
*/publicList<Widget> getWidgets() {
return widgets;
}
/**
*
* @paramwidgets
* The widgets
*/publicvoidsetWidgets(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")
publicclassWidget {
@SerializedName("title")
@ExposeprivateString title;
@SerializedName("desc")
@ExposeprivateString desc;
/**
*
* @return
* The title
*/publicStringgetTitle() {
return title;
}
/**
*
* @paramtitle
* The title
*/publicvoidsetTitle(String title) {
this.title = title;
}
/**
*
* @return
* The desc
*/publicStringgetDesc() {
return desc;
}
/**
*
* @paramdesc
* The desc
*/publicvoidsetDesc(String desc) {
this.desc = desc;
}
}
Now build the retrofit object:
RetrofitmRetrofit=newRetrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .baseUrl(Constants.BASE_INTERNAL_SERVER_ADDRESS) .build();
And refer to correspondin endPoint, but first call your progress dialog:
dialog = newProgressDialog(Parser.this); dialog.setMessage("Pasring..."); dialog.setIndeterminate(true); dialog.setCancelable(true); dialog.show(); Call<Example> fetchWidgets = mRetrofit.create(InternalServerAPI.class).healthcheckEndPoint(Params); fetchWidgets.enqueue(newCallback<Example>() { @OverridepublicvoidonResponse(Call<Example> call, Response<Example> response) { //here response is your model object and U can refer to its fieldsArrayList<Widget> data = response.getWidgets(); ... //update adapter ... //and now U can dismiss your dialog dialog.dismiss(); } @OverridepublicvoidonFailure(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?"