im using OptimusHTTP library on my android project. im trying to show loading if my app is contacting server. my problem is why my progress dialog does not dismiss. here is my code.
public void connectREST()
{
//using data json dummy
String SERVER = "http://jsonplaceholder.typicode.com/posts/1";
OptimusHTTP client = new OptimusHTTP();
client.enableDebugging();
client.setMethod(OptimusHTTP.METHOD_GET);
//parameter
ArrayMap<String, String> params = new ArrayMap<>();
params.put("email", "abc@abc.com");
params.put("pass", "abc");
//make request
ArrayList<HttpReq> refHttpReqList = new ArrayList<>();
try {
//mprogressdialog.show(this, "", "Loading", true);
// makeRequest() returns the reference of the request made
// which can be used later to call the cancelReq() if required
// if no request is made the makeRequest() returns null
HttpReq req = client.makeRequest(MainActivity.this, SERVER, params, responseListener);
if (req != null)
refHttpReqList.add(req);
mprogressdialog.show(this, "Loading", "Wait while loading...");
if (mprogressdialog != null && mprogressdialog.isShowing()) {
mprogressdialog.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private final OptimusHTTP.ResponseListener responseListener = new OptimusHTTP.ResponseListener() {
@Override
public void onSuccess(String msg) {
System.out.println(msg);
//mprogressdialog.dismiss();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(String msg) {
System.out.println(msg);
}
};
i know that this library (OptimusHTTP) is using asnyc when contacting server. is there any configuration whether im using sync or async on the http connection ? what if im include get method in some async code (double async) ?
i know that my question seem like some newbie question. but it takes learn process to become a pro :) Thanks.
@navotera : You can show a ProgressDialogue just before making a request and when the request completes , under the listener just dismiss the progress dialogue.
i.e
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
// Initialize the progressdialog
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Connecting");
...
...
// Show the progressdialog just before making a request
progressDialog.show();
// Make the request
req = client.makeRequest(MainActivity.this, SERVER_URL, params,new OptimusHTTP.ResponseListener(){
@Override public void onSuccess(String msg) {
System.out.println(msg);
// Dismiss the progressdialog
progressDialog.dismiss();
}
@Override public void onFailure(String msg) {
System.out.println(msg);
// Dismiss the progressdialog
progressDialog.dismiss();
}
});