I have a JSONparser Class to get and send data to a server which works fine but when testing this without a wifi connection the process takes a little longer. Is it possible to put a process Dialog into my class, as I would call this class form each activity that requires data to be sent or received.
I have tried a few different things like applying setting the visisability of a LinearLayout before and after the task like:
loading.setVisibility(View.VISIBLE);
/// DO TASK
loading.setVisibility(View.GONE);
But the screen just freezes and loads the data in anyway.
I have tried adding a processDialog at the start of the HTTP request and removing it again when the task is complete but I get a null reference error.
I have feeling that the error may lies in the class itself, as I am new to Java I only really know the basics at the moment so just learning.
This is my JSONParser Class
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
static String root = "**MY SERVER**";
private View loading = null;
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
params.add(new BasicNameValuePair("APP_ID", "**APPTOKEN**"));
// Making HTTP request
try {
// check for request method
if(method.equalsIgnoreCase("POST")){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(this.root + url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method.equalsIgnoreCase("GET")){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Truth be told I did not write all of that class, I followed a tutorial and amended the class to my needs.
The class can be called in any activity, which was the idea of the class like this:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONParser jsonParser = new JSONParser();
loading.setVisibility(View.VISIBLE);
JSONObject json = jsonParser.makeHttpRequest("login.php", "POST", params);
try {
Boolean success = json.getBoolean("ok");
loading.setVisibility(View.GONE);
if (success) {
Log.d("LOGIN","LOGIN SUCCESSFUL");
finish();
} else {
String err_msg = json.getString("error");
Toast toast = Toast.makeText(getApplicationContext(), err_msg, Toast.LENGTH_LONG);
toast.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
If the problem is the class, which I'm sure it is could you offer an explanation of how to amend this to incorporate a progressDialog.
UPDATE With help in the answer below I have managed to add a process dialog using a PostAsync class. Ass I have modified this class to be more dynamic is it possible to process the return on my current activity or can I only process the return inside the PostAcync Class.
For example: The basic PostAcync Class would be called by New PostAcync.execute(param,param); But i have modified this so that it is universal and can work on any activity; So no instead I would call this class and execute the task by:
PostAsync post = new PostAsync();
post.context = ThisActivity.this;
post.message = "Attempting to login";
post.execute("login.php", email, password);
So now I add in the Context for the Dialog Builder to function I can add a different message depending on the task And the first param is always the webpage.
is there a way I can add a callback onto this something like
JSONObject response = post.repsonse;
//then process the data here as I could using the ajax success callback in jQuery