My class reads a json string from web and with collected string populate an hashmap. I'm using AsyncTask to read data with a progressdialog to let the user that the devise "is working". This is my class:
class ArmoryAsyncProgress extends AsyncTask<String, Void, Void> {
private Context mContext;
private ProgressDialog mProgressDialog;
private String tempRes;
public ArmoryAsyncProgress(Context mContext)
{
this.mContext = mContext;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = ProgressDialog.show(mContext, "Carica!", "Gira!");
}
@SuppressLint("NewApi")
@Override
protected String doInBackground(String... sUrl) {
try {
URL json = new URL(Utility.BASE_URL + "api.php?action=armory&guid="+pGuid);
BufferedReader in = new BufferedReader(
new InputStreamReader(
json.openStream()));
String input;
while((input = in.readLine()) != null)
Result += input;
json = new URL(Utility.BASE_URL + "api.php?action=armory_stats&guid="+pGuid);
in = new BufferedReader(
new InputStreamReader(
json.openStream()));
input = "";
String ret = "";
while((input = in.readLine()) != null)
ret += input;
tempRes = Result + "Ø" + ret;
String debug = tempRes;
}
catch(MalformedURLException e){
e.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
ActivityArmory.result = tempRes;
return null;
}
protected void onPostExecute(String result) {
/*********************************************************************************/
try
{
String ret;
while(result == null)
Thread.sleep(500);
String[] temp = result.split("Ø");
pJSON = temp[0];
ret = temp[1];
JSONObject pl = new JSONObject(ret);
stats.put("Level", pl.getString("level"));
stats.put("Classe", pl.getString("class"));
stats.put("Name", pl.getString("pname"));
stats.put("Race", pl.getString("race"));
stats.put("health", pl.getString("health"));
stats.put("power", pl.getString("power1"));
stats.put("gname", pl.getString("gname"));
stats.put("pnote", pl.getString("pnote"));
stats.put("offnote", pl.getString("offnote"));
stats.put("rname", pl.getString("rname"));
JSONArray jObject = new JSONArray(pJSON);
for (int i = 0; i < jObject.length(); i++) {
JSONObject item = jObject.getJSONObject(i);
ArmoryElement i1 = new ArmoryElement(
"http://wow.zamimg.com/images/wow/icons/large/" + item.getString("itemIMG") + ".jpg",
item.getInt("itemID"), item.getInt("quality"));
el.put(item.getString("itemType"), i1);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
/****************************************************************/
mProgressDialog.dismiss();
}
}
and this is the call to the class:
new ArmoryAsyncProgress().execute(pGuid+"");
After the "execute" method call i call another function, used to format and display data fetched from web. My problem is that the progressDialog declared in the Async class is not showed and the function called after execute gets called before the execution is finished (Using some Log i found that the log in the second function are displayed before the doInBackground finish)
I tried the .get mothod too, it freezes the main thread and prevent the function from being called, but i can't show the progressdialog.
Thanks in advance