문제

I have a problem wih my progressdialog, my app searches for the data from json and after that the dialog comes, but I want it at this time while he collects the data. where is my mistake?

There is the edited code logcat( FATAL EXCEPTION: main java.lang.NullPointerException) JSON ([{"name":"Test"}]) edit:

public class MainActivity extends ListActivity {

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

    new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, JSONObject> {
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    ProgressDialog pd;

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        pd.dismiss();
        JSONObject jObject = result;
        try {
            String aJsonString = jObject.getString("name");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, (List<String>) result));
        // parse and set List adapter here
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(MainActivity.this, "dialog title",
                "dialog message", true);
    }

    @Override
    protected JSONObject doInBackground(Void... arg0) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("***");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        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();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}
도움이 되었습니까?

해결책

Thread.sleep(50);

You are calling sleep on the ui thread which blocks it. Remove it.

Use AsyncTask instead of a Thread.

http://developer.android.com/reference/android/os/AsyncTask.html

Show progress dialog in onPreExecute. Do your http request in doInbackground. Dismiss dialog in onPostExecute and update ui accordingly.

To invoke

 new TheTask().execute();

Then

 class TheTask extends AsyncTask<Void, Void, Void>    {

        ProgressDialog pd;

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progress.dismiss();

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
             progress = ProgressDialog.show(MainActivity.this, "dialog title",
                            "dialog message", true);
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // http request
            return null;
        }   
    } 

Edit:

public class MainActivity extends ListActivity {

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

    new TheTask().execute();
    }
class TheTask extends AsyncTask<Void, Void, JSONObject> {
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    ProgressDialog pd;

    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        pd.dismiss();
        // parse and set List adapter here
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(MainActivity.this, "dialog title",
                "dialog message", true);
    }

    @Override
    protected JSONObject doInBackground(Void... arg0) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("******");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        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();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}
}

Edit2:

   class TheTask extends AsyncTask<Void, Void, JSONArray> {
    InputStream is = null;
    String result = "";
    JSONArray jArray = null;

    ProgressDialog pd;

    @Override
    protected void onPostExecute(JSONArray result) {
        super.onPostExecute(result);
        pd.dismiss();
        ArrayList<String> list= new ArrayList<String>();
        try
        {
        for(int i=0;i<result.length();i++)
        {

            JSONObject jb = result.getJSONObject(i) ;
            String name = jb.getString("name");
            list.add(name);
        }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list));
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(MainActivity.this, "dialog title",
                "dialog message", true);
    }

    @Override
    protected JSONArray doInBackground(Void... arg0) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("******");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        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();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONArray(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}
}

다른 팁

You call Thread.sleep(50) on the UI Thread so the UI freezes until its done. Move your operations to an AsyncTask and sleep() in doInBackground(). Put all of your network code in doInBackground() also.

You can use AsyncTasks other methods to update the UI.

See this answer for an example of AsyncTask if you are unfamiliar

AsyncTask Docs

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top