我如何在onPostExecute()AsyncTask android中获得响应

时间:2015-04-02 11:05:06

标签: android web-services android-asynctask

我最近开始研究android,现在我正在使用AsyncTask如何获得从下面的API返回的响应是代码。每个建议都很明显。

 class signmeup extends AsyncTask<String, String, String> {

        private ProgressDialog pDialog;
        JSONParser jsonParser = new JSONParser();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(WolfActivity.this);
            pDialog.setMessage("Loading");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("section","user"));
            params.add(new BasicNameValuePair("action","new"));
            params.add(new BasicNameValuePair("device_type","2"));
            params.add(new BasicNameValuePair("device_token","dhdkhgkdfhgkhfdghkdfjhgkjdfhgkdfhkhkHKhdkhsdkhKJHKWJHDSKAHDKJSAHJKDfhkashfkdjhfkjhskjhKJHJk"));
            params.add(new BasicNameValuePair("first_name",fname));
            params.add(new BasicNameValuePair("last_name",lname));
            params.add(new BasicNameValuePair("email",email));
            params.add(new BasicNameValuePair("phone",phone));
            params.add(new BasicNameValuePair("fax",fax));
            params.add(new BasicNameValuePair("address",addr));
            params.add(new BasicNameValuePair("address1",addr1));
            params.add(new BasicNameValuePair("facility",facility));
            params.add(new BasicNameValuePair("password",pwd));
            params.add(new BasicNameValuePair("zip",zipcode));
            params.add(new BasicNameValuePair("city",city));
            params.add(new BasicNameValuePair("state",state));
            params.add(new BasicNameValuePair("how_you_found",huf));
            params.add(new BasicNameValuePair("how_you_found_value",hufv));
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest("http://eastendwebsolutions.com/wf/ws/", "GET", params);

            Log.d("First Name",fname);
            Log.d("Last Name",lname);
            Log.d("Email",email);
            // check log cat for response
            Log.d("Create Response", json.toString());

            // check for success tag
           try {
                String success = json.getString("status");
                String message = json.getString("message");


               if(success == "0") {
                    Toast.makeText(WolfActivity.this,message ,Toast.LENGTH_LONG).show();
                } else {
                   Toast.makeText(WolfActivity.this,"Error "+message ,Toast.LENGTH_LONG).show();
               }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @SuppressWarnings("deprecation")
        //@Override
        protected void onPostExecute(String Result) {

            if(pDialog.isShowing()){
                pDialog.dismiss();
            }
            Log.d("Result",Result);
            AlertDialog.Builder builder=new AlertDialog.Builder(WolfActivity.this);
            builder.setTitle("Info");
            builder.setMessage("Successfully registered");
            builder.setIcon(R.drawable.app_icon);
            builder.create().show();

        }

    }

以上代码以错误终止

  

java.lang.NullPointerException:尝试调用虚方法&#39; java.lang.String java.lang.String.toString()&#39;在null对象引用上               在zonup.wolf.WolfActivity $ signmeup.onPostExecute(WolfActivity.java:222)               在zonup.wolf.WolfActivity $ signmeup.onPostExecute(WolfActivity.java:135)

     

错误,因为此语句为Log.d(&#34; Result&#34;,Result);在功能上   onPostExecute(String Result)

我试图用doInBackground()方法提醒用户状态,但没有吐司即将来临:(它是否正确?

1 个答案:

答案 0 :(得分:2)

试试这种方式,你可以看到你在json响应中得到的消息。

 class signmeup extends AsyncTask<String, String, String> {


     String message="";

            private ProgressDialog pDialog;
            JSONParser jsonParser = new JSONParser();

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(WolfActivity.this);
                pDialog.setMessage("Loading");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected String doInBackground(String... arg0) {
                // TODO Auto-generated method stub

                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("section","user"));
                params.add(new BasicNameValuePair("action","new"));
                params.add(new BasicNameValuePair("device_type","2"));
                params.add(new BasicNameValuePair("device_token","dhdkhgkdfhgkhfdghkdfjhgkjdfhgkdfhkhkHKhdkhsdkhKJHKWJHDSKAHDKJSAHJKDfhkashfkdjhfkjhskjhKJHJk"));
                params.add(new BasicNameValuePair("first_name",fname));
                params.add(new BasicNameValuePair("last_name",lname));
                params.add(new BasicNameValuePair("email",email));
                params.add(new BasicNameValuePair("phone",phone));
                params.add(new BasicNameValuePair("fax",fax));
                params.add(new BasicNameValuePair("address",addr));
                params.add(new BasicNameValuePair("address1",addr1));
                params.add(new BasicNameValuePair("facility",facility));
                params.add(new BasicNameValuePair("password",pwd));
                params.add(new BasicNameValuePair("zip",zipcode));
                params.add(new BasicNameValuePair("city",city));
                params.add(new BasicNameValuePair("state",state));
                params.add(new BasicNameValuePair("how_you_found",huf));
                params.add(new BasicNameValuePair("how_you_found_value",hufv));
                // getting JSON Object
                // Note that create product url accepts POST method
                JSONObject json = jsonParser.makeHttpRequest("http://eastendwebsolutions.com/wf/ws/", "GET", params);

                Log.d("First Name",fname);
                Log.d("Last Name",lname);
                Log.d("Email",email);
                // check log cat for response
                Log.d("Create Response", json.toString());

                // check for success tag
               try {
                    String success = json.getString("status");
                     message = json.getString("message");


                   if(success == "0") {
                        Toast.makeText(WolfActivity.this,message ,Toast.LENGTH_LONG).show();
                    } else {
                       Toast.makeText(WolfActivity.this,"Error "+message ,Toast.LENGTH_LONG).show();
                   }
                } catch (JSONException e) {

                    message=e.toString();
                    e.printStackTrace();
                }

                return message;
            }

            @SuppressWarnings("deprecation")
            //@Override
            protected void onPostExecute(String Result) {

                if(pDialog.isShowing()){
                    pDialog.dismiss();
                }
                Log.d("Result",Result);
                AlertDialog.Builder builder=new AlertDialog.Builder(WolfActivity.this);
                builder.setTitle("Info");
                builder.setMessage(Result);
                builder.setIcon(R.drawable.app_icon);
                builder.create().show();

            }

        }