0

我想在從服務器獲取響應後執行http post。 如果服務器響應爲false,那麼http post將執行,否則不執行。 我該怎麼辦。Android線程完成回調

我的Android主要活動代碼:

if (Utility.isValidMobile(mobileNumber)) { 
      String isAvailable = userDelegate.checkUser(mobileNumber, context); 


      if (isAvailable.equals("false")) { 
       userDelegate.addUser(userMO, context); 
       Toast.makeText(getApplicationContext(), "Your mobile number is" + mobileNumber + "name is" + userName, Toast.LENGTH_LONG).show(); 
      } else if (isAvailable.equals("true")) { 
       Toast.makeText(getApplicationContext(), "Your mobile number is already registerd", Toast.LENGTH_LONG).show(); 
      } 
     } 

當我點擊註冊按鈕,這上面的代碼將執行

我Userdelegate類代碼:

public void addUser(final UserMO userMo, final Context context) { 

    final String jsonStringObject = gson.toJson(userMo); 

    Thread t = new Thread() { 
     public void run() { 
      Looper.prepare(); // for the child Thread 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
                        // Limit 
      try { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("userBO", jsonStringObject)); 
       HttpPost post = new HttpPost("http://192.168.1.101:8080/warname/user/addUser"); 
       post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = client.execute(post); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
       Toast.makeText(context, "Your user id " + rd.readLine(), Toast.LENGTH_LONG).show(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      Looper.loop(); // Loop in the message queue 
     } 
    }; 
    t.start(); 

} 

public void getMatchingExistingUserList(final String mobile_number, final Context context) { 

    final String jsonStringObject = gson.toJson(mobile_number); 

    Thread t = new Thread() { 
     public void run() { 
      Looper.prepare(); // for the child Thread 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
                        // Limit 
      try { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("userBO", jsonStringObject)); 
       HttpPost post = new HttpPost("http://192.168.1.101:8080/warname/user/addUser"); 
       post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = client.execute(post); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
       final String responseString = rd.readLine(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      Looper.loop(); // Loop in the message queue 
     } 
    }; 
    t.start(); 
} 

public String checkUser(final String mobile_number, final Context context) { 

    final StringBuilder isAvailable = new StringBuilder(); 

    Thread t = new Thread() { 
     @Override 
     public void run() { 
      Looper.prepare(); // for the child Thread 
      try { 
       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("MobileNumber", gson.toJson(mobile_number))); 
       HttpPost post = new HttpPost("http://192.168.1.101:8080/warname/user/checkUserMobileNumber"); 
       post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = client.execute(post); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
       isAvailable.append(rd.readLine()); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      Looper.loop(); // Loop in the message queue 
     } 
    }; 
    t.start(); 
    return isAvailable.toString(); 
} 

問題是,我得到了迴應假,但如果條件不起作用。 如何解決這個問題。

變化後:

if (Utility.isValidMobile(mobileNumber)) { 
      new AsyncTask<Void, Void, String>() { 
       @Override 
       protected String doInBackground(Void... arg0) { 
        return userDelegate.checkUser(mobileNumber, context); 
       } 

       @Override 
       protected void onPostExecute(String isAvailable) { 
        Toast.makeText(getApplicationContext(), isAvailable, Toast.LENGTH_LONG).show(); 
        if (isAvailable.equals("false")) { 
         Toast.makeText(getApplicationContext(), "Your mobile number is" + mobileNumber + "name is" + userName, Toast.LENGTH_LONG).show(); 
         userDelegate.addUser(userMO, context); 
        } else if (isAvailable.equals("true")) { 
         Toast.makeText(getApplicationContext(), "Your mobile number is already registerd", Toast.LENGTH_LONG).show(); 
        } 
       } 
      }.execute(null, null, null); 
     } 

if條件不工作?

+0

它給出了什麼錯誤?這個'isAvailable.equals(「false」)的值是多少?' – 2015-02-06 05:31:18

+0

但isAvailable值是false。 – nmkkannan 2015-02-06 07:18:03

回答

1

如果服務器響應爲false,則http post將執行else else execute。我該怎麼辦

問題發生是因爲您正在使用checkUseraddUser中的線程。線程執行停止執行當前線程。

例如,當從主線程調用checkUser方法,然後在主線程上執行final StringBuilder isAvailable = new StringBuilder();,並且線程t正在單獨執行。所以系統返回控制到下一行即return isAvailable.toString();而不等待線程執行完成意味着checkUser方法總是返回nullempty字符串。

相同的是addUser方法。

做任務根據checkUser方法響應使用AsyncTask類的結果。

+0

Thx幫助我改變它。 – nmkkannan 2015-02-06 06:34:22

+0

我想要像我的代碼的例子嗎? – nmkkannan 2015-02-06 06:35:19

+0

@nmkkannan:好的,做你的自我然後我一定會盡力幫你 – 2015-02-06 06:43:54

2

您在此處使用新線程執行http請求。因此,您的委託方法不同步。 addUsercheckUser將在您的http請求完成之前返回。

要編寫像您這樣的多線程代碼,您可能需要使用某種偵聽器來執行線程通信工作。

例如,你可以傳遞一個監聽器,你的委託,看起來像這樣

class Listener{ 
    private Handler handler = new Handler(); 
    public void onUserAdded(){ 
     handler.post(new Runnable(){ 
       public void run(){ 
        // Toast your thing 
       } 
     }); 
    } 

    public void onUserChecked(final boolean available){ 
     handler.post(new Runnable(){ 
       public void run(){ 
        if(available){ 
          // Toast your thing 
        }else{ 
          userDelegate.addUser(userMO, context); 
        } 
       } 
     }); 
    } 
} 

和所有new Thread(){ run(){代碼應與所有呼叫監聽結束。

正如您所看到的,我使用Handler將作品發回到UI線程。這對你來說非常重要,可以告訴你的UI元素你的非UI線程正在發生什麼。

此外,我不明白你在做什麼與你的Looper.prepare()Looper.loop()。沒有子線程在那裏。

+0

Thx幫助它非常有用.. – nmkkannan 2015-02-06 06:34:00