Progress bar auto hide in debug mode, but not in a simply run

Home / android / Progress bar auto hide in debug mode, but not in a simply run

Question:
Unexpected issue I’m getting while run my android app. I call rest API with using progress bar AsyncTask. Somehow progress load correctly and hide correctly while I’m in debugging mode. When run my app without debugging mode the progress load correctly but not hide automatically. Below is my code

‘MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
TextView tv1;
ProgressDialog pd;
Button Btngetdata;
String myJSON;

//URL to get JSON Array
// protected static String url = "http://192.168.1.104:856/api/Quotes/GetQuote/EN";

JSONArray user = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textviewquotes);
Btngetdata = (Button) findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
getData();
// new JSONParse().execute("http://192.168.1.104:856/api/Quotes/GetQuote/EN");
String url = "http://192.168.1.104:856/api/Quotes/GetQuote/EN";
}
});
}

public void getData() {
Log.w("Tag", "Get Call");

JSONParse g = new JSONParse();
g.execute("");
Log.w("Tag", "Async call end");
}

public void showList() {
String emplist = "";
try {
JSONObject jobj = new JSONObject(myJSON);
String s = jobj.getString("QuoteText");
tv1.setText(s);
} catch (Exception e) {

}

}

private class JSONParse extends AsyncTask {
private ProgressDialog pDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();

/* pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Please wait");
pd.setCancelable(false);
pd.show();*/

// pd.dismiss();
}

@Override
protected String doInBackground(String… params) {
String xeno = params[0];
List nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("QuoteID", xeno));
InputStream inputStream = null;
String result = null;
try {
android.os.Debug.waitForDebugger();
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());

HttpGet httpPost = new HttpGet("http://192.168.1.104:856/api/Quotes/GetQuote/EN");
// httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
Log.i("Tag", sb.toString());
result = sb.toString();

} catch (Exception e) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
return "Error from remote server";
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception ex) {

}
}
return result;
}

@Override
protected void onPostExecute(String s) {
/* if (pd.isShowing()) {
pd.dismiss(); */

myJSON = s;
showList();
}
}
}’
Thanks for helping 🙂 I’m new bee in Android.


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *