I want to show progress of some JSON parsing using with progress bar. I've never used it and found some examples in the Internet. So, I try to realize it but application crashes when parsing starts. Here is code:
public class Parser extends Activity {
public static String w_type1 = "news";
public static String w_type2 = "events_put";
public ListView lv;
ArrayList<Widget> data = new ArrayList<Widget>();
WidgetAdapter wid_adptr = new WidgetAdapter(this, data);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parser);
lv = (ListView) this.findViewById(R.id.list);
lv.setAdapter(wid_adptr);
new ParseTask().execute();
}
private class ParseTask extends AsyncTask<Void, Void, String> {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String resultJson = "";
public ProgressDialog dialog;
Context ctx;
protected void onPreExecute() {
dialog = new ProgressDialog(ctx);
dialog.setMessage("Pasring...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL("http://api.pandem.pro/healthcheck/w/");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resultJson = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return resultJson;
}
@Override
protected void onPostExecute(String strJson) {
super.onPostExecute(strJson);
JSONObject dataJsonObj = null;
try {
dataJsonObj = new JSONObject(strJson);
JSONArray widgets = dataJsonObj.getJSONArray("widgets");
for (int i = 0; i < widgets.length(); i++) {
JSONObject widget = widgets.getJSONObject(i);
String wType = widget.getString("type");
if (wType.equals(w_type1) || wType.equals(w_type2)) {
String title = widget.getString("title");
String desc = widget.getString("desc");
String img_url = "";
if (widget.has("img")) {
JSONObject img = widget.getJSONObject("img");
img_url = img.getString("url");
}
data.add(new Widget(wType, title, desc, img_url));
//wid_adptr.notifyDataSetChanged();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
dialog.dismiss();
}
}
}
If i don't use ProgressDialog (just comment or delete dialog code) application works correctly. How can I fix it?
Without any logcat it's difficult to help, but it seems that your
ctx
is null sothe dialog can't be created.
Try to add constructor to the
AsyncTask
and pass the context here, something like:To start the task:
Well, I suppose this can be done in much easier and modern way.
Now step by step:
add dependencies to Your gradle file:
this will let U use retrofit lib 2. Create retrofit serverAPI interface
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:
}
U`ll obtain two model classes like this:
Now build the retrofit object:
And refer to correspondin endPoint, but first call your progress dialog:
Of course all of this should be done in some MVP way, but it is not a subject now.