-1

I want to parse data using JSON with showing Progress bar.This code working fine without progress Bar but when i use progress Bar then its Unfortunately Stopped!!!

Here is my MainActivity.java code

public class MainActivity extends Activity {
private ProgressDialog pDialog;

//URL to get JSON Array
private static String url = "https://dl.dropboxusercontent.com/u/131328281/admission.json";

//JSON Node Names 
private static final String TAG_ADMISSION = "admission";
private static final String TAG_DATE = "date";
private static final String TAG_TITLE = "title";
private static final String TAG_GENINFO = "geninfo";
private static final String TAG_GENINFO2 = "geninfo2";
private static final String TAG_APPINCLUDE = "appinclude";
private static final String TAG_APPINCLUDE2 = "appinclude2";
private static final String TAG_UNDERGRAD = "undergrad";
private static final String TAG_UNDERGRAD2 = "undergrad2";
private static final String TAG_POSTGRAD = "postgrad";
private static final String TAG_POSTGRAD2 = "postgrad2";
private static final String TAG_ADMSN = "admsn";
private static final String TAG_ADMSN2 = "admsn2";
JSONArray admission = null;



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

    setContentView(R.layout.adm);
    new DownloadTask().execute();
}
private class DownloadTask extends AsyncTask<String, Void, Void> {



    /* Start the progress in onPreExecute */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         pDialog = new ProgressDialog(getApplicationContext()); 
            pDialog.setMessage("Loading ...Please Wait..");
            pDialog.setIndeterminate(false);
            pDialog.setCanceledOnTouchOutside(false);
            pDialog.setCancelable(true);
            pDialog.show();
    }
    protected Void doInBackground(String... params) {
    // Creating new JSON Parser
    JSONParser jParser = new JSONParser();

    // Getting JSON from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting JSON Array
        admission = json.getJSONArray(TAG_ADMISSION);
        JSONObject c = admission.getJSONObject(0);

        // Storing  JSON item in a Variable
        String date = c.getString(TAG_DATE);
        String title = c.getString(TAG_TITLE);
        String geninfo = c.getString(TAG_GENINFO);
        String geninfo2 = c.getString(TAG_GENINFO2);
        String appinclude = c.getString(TAG_APPINCLUDE);
        String appinclude2 = c.getString(TAG_APPINCLUDE2);
        String undergrad = c.getString(TAG_UNDERGRAD);
        String undergrad2 = c.getString(TAG_UNDERGRAD2);
        String postgrad = c.getString(TAG_POSTGRAD);
        String postgrad2 = c.getString(TAG_POSTGRAD2);
        String admsn = c.getString(TAG_ADMSN);
        String admsn2 = c.getString(TAG_ADMSN2);

        //Importing TextView
        final TextView name2 = (TextView)findViewById(R.id.geninfo);
        final TextView name3 = (TextView)findViewById(R.id.geninfo2);
        final TextView name4 = (TextView)findViewById(R.id.app_include);
        final TextView name5 = (TextView)findViewById(R.id.app_include2);
        final TextView name6 = (TextView)findViewById(R.id.undergrad);
        final TextView name7 = (TextView)findViewById(R.id.undergrad2);
        final TextView name8 = (TextView)findViewById(R.id.postgrad);
        final TextView name9 = (TextView)findViewById(R.id.postgrad2);
        final TextView name10 = (TextView)findViewById(R.id.admsn);
        final TextView name11= (TextView)findViewById(R.id.admsn2);

        final Button btn = (Button)findViewById(R.id.button1);

        //Set JSON Data in TextView
        btn.setText(title + "\n" + date);
        name2.setText(geninfo);
        name3.setText(geninfo2);
        name4.setText(appinclude);
        name5.setText(appinclude2);
        name6.setText(undergrad);
        name7.setText(undergrad2);
        name8.setText(postgrad);
        name9.setText(postgrad2);
        name10.setText(admsn);
        name11.setText(admsn2);



} catch (JSONException e) {
    e.printStackTrace();
}
return null;
}
        protected void onPostExecute(Void Void) {
            pDialog.dismiss();
        }
  }

This is adm.xml

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/color_buttons"
    android:textColor="#0059b2"
    android:textSize="20dp"
    android:textStyle="bold"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"/>
<TextView
    android:id="@+id/geninfo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="100dp"
    android:layout_below="@+id/Button1"
    android:textSize="15dp"
    android:textStyle="bold"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/geninfo2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/geninfo"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/app_include"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/geninfo2"
    android:textStyle="bold"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/app_include2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/app_include"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/undergrad"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:layout_below="@+id/app_include2"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/undergrad2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/undergrad"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/postgrad"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:layout_below="@+id/undergrad2"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/postgrad2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/postgrad"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/admsn"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:layout_below="@+id/postgrad2"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/admsn2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/admsn"
    android:textSize="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

Here is JSONPaser.java

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        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, "iso-8859-1"), 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;

}

}

  • There appears to be a number of things wrong here. You haven't declared your ProgressBar in the xml, you set it as indeterminate(false) but you don't set its initial value to zero and there's no callback in your task to do the updating. – John J Smith Aug 17 '14 at 10:35

1 Answers1

0

Instead of Using getApplicationContext for Progress dialog, try using Activity context, this should solve your issue.

Dinash
  • 3,027
  • 4
  • 32
  • 45