2015-04-02 109 views
0

我最近開始在android上工作,現在我正在使用AsyncTask我如何得到從API返回的響應是code.Every建議是可觀的。我如何獲得響應onPostExecute()AsyncTask android

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:嘗試一個空對象上調用虛擬方法java.lang.String中java.lang.String.toString()'在zonup.wolf.WolfActivity $ signmeup.onPostExecute(WolfActivity.java:222) 在zonup.wolf.WolfActivity $ signmeup.onPostExecute(WolfActivity.java:135)

錯誤引用 因爲這種說法Log.d(」結果」,結果);在功能 onPostExecute(字符串結果)

我試圖提醒在doInBackground()方法與狀態用戶,但沒有敬酒來了:(是不是正確的?

+0

從'doInBackground'。意味着返回''null'是Result'空 – 2015-04-02 11:06:29

+0

所以我必須返回字符串,轉換/類型從JSON響應鑄造的吧?@ρяσѕρєяK – 2015-04-02 11:09:46

+0

@ manikanta g:是return'json' a nd show來自'onPostExecute'方法的Toast和通知 – 2015-04-02 11:11:49

回答

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(); 

      } 

     }