从JSON获取数据时,应用程序崩溃

时间:2015-04-30 20:38:36

标签: java android json parsing

我试图从JSON检索数据,但每当我尝试从Android应用程序检索数据时它就会崩溃。

//  Intent i = new Intent(this,MainMenu.class);
//  startActivity(i);

new AsyncTask<Void, Void, Void>()
{
    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressDialog = new ProgressDialog(JobScreen.this);
        progressDialog.setMessage("Getting Items..");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... voids)
    {
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpGet post = new HttpGet("http://users.abdullahadhaim.com/users/WebServiceResturant.asmx/login?userName=abood&Password=123");
            HttpResponse response = httpClient.execute(post);
            String responseString = EntityUtils.toString(response.getEntity());
            JSONArray jsonArray = new JSONArray(responseString);

                JSONObject jsonObject = jsonArray.getJSONObject(0);
                ed1.setText(jsonObject.getString("UserName"));



            Log.e("Done", "Done");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            Toast.makeText(JobScreen.this, "Faild", Toast.LENGTH_SHORT).show();

        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid)
    {
        super.onPostExecute(aVoid);
        progressDialog.dismiss();


    }

}.execute();

1 个答案:

答案 0 :(得分:0)

看起来问题是你在后台线程中调用了ed1.setText()

只需将该调用移至onPostExecute(),然后从String返回所需的doInBackground()值。

同时从Toast移除doInBackground(),如果onPostExecute()的返回值为空,则将其移至doInBackground()以显示;

我刚刚运行了它,它工作正常,并将文本设置为abood

 //use String for last parameter here:
 new AsyncTask<Void, Void, String>() {
        ProgressDialog progressDialog;

        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(JobScreen.this);
            progressDialog.setMessage("Getting Items..");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
        //String return value:
        protected String doInBackground(Void... unused) {


            try
            {
                DefaultHttpClient httpClient = new DefaultHttpClient();

                HttpGet post = new HttpGet("http://users.abdullahadhaim.com/users/WebServiceResturant.asmx/login?userName=abood&Password=123");
                HttpResponse response = httpClient.execute(post);
                String responseString = EntityUtils.toString(response.getEntity());
                JSONArray jsonArray = new JSONArray(responseString);

                JSONObject jsonObject = jsonArray.getJSONObject(0);


                Log.e("Done", "Done");

                //return the String you need:
                return jsonObject.getString("UserName");
            }
            catch (Exception e)
            {
                e.printStackTrace();
                //remove this Toast:
                //Toast.makeText(MainActivity.this, "Faild", Toast.LENGTH_SHORT).show();

            }

            return null;

        }
        //String parameter
        protected void onPostExecute(String username) {
            super.onPostExecute(username);

            if (username == null){
                //Toast if username is null
                Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
            }
            else{
               //Set the text here with the String received:
               ed1.setText(username);
            }    
            progressDialog.dismiss();
        }
    }.execute();