2012-09-21 25 views
1

我有我的MainAcitivity內部下面的代碼使用方法jsnrqstr.sendRequest(username, password, URL);如何使用HTTPPost沒有新的可運行線程

loginButton.setOnClickListener(
     new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 
       username = userNameBox.getText().toString(); 
       password = passwordBox.getText().toString(); 
       login_state = jsnrqstr.sendRequest(username, password, URL); 
       if(login_state.equals("ok")){ 
        details.setUserName(username); 
        details.setPassword(password); 
        Toast.makeText(MainActivity.this, "User Verified", Toast.LENGTH_LONG).show(); 
        passwordBox.setText(""); 
        Intent myIntent = new Intent(MainActivity.this, DashBoardActivity.class); 
        MainActivity.this.startActivity(myIntent); 
       }else 
        Toast.makeText(MainActivity.this, "Login Unsuccessful", Toast.LENGTH_LONG).show(); 
        passwordBox.setText(""); 
      } 
     }); 

此方法使用下面的代碼,用於接收拍攝的用戶名和密碼登錄到應用程序從一個Erlang服務器響應該響應與JSON對象,我可以從它接收login_state。代碼jsnrqstr.sendRequest(username, password, URL);

public class JSONRequester { 

String state; 

public String sendRequest(final String userName, final String password, final String URL){ 

    new Thread(new Runnable() { 
     public void run() { 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
      HttpResponse response; 
      JSONObject json = new JSONObject(); 
      try{ 
       HttpPost post = new HttpPost(URL); 
       post.setHeader("Content-type", "application/json"); 
       json.put("username", userName); 
       json.put("password", password); 
       StringEntity se = new StringEntity(json.toString()); 
       se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
       post.setEntity(se); 
       response = client.execute(post); 
       /*Checking response */ 
       if(response!=null){ 
        String temp = EntityUtils.toString(response.getEntity()); //Get the data in the entity 
        //Log.v("response", temp); 
        JsonObject o = new JsonParser().parse(temp).getAsJsonObject(); 
        Log.v("response", o.get("state").toString()); 
        state = o.get("state").toString(); 
      }} 
      catch(Exception e){ 
       e.printStackTrace(); 
       //createDialog("Error", "Cannot Estabilish Connection"); 
      } 
     } 
     }).start(); 
      return state; 
} 

但我得到以下空指針異常 @

if(login_state.equals("ok")){ 

線,這意味着我沒有正確地返回從上述方法login_state值。因爲我認爲這是因爲當時的新運行的線程返回值爲時已晚。我能做些什麼來解決這個問題?

回答

1

的問題是線:)之間 - 這兩個:

})開始();

< == HERE !!!

return state; 

第二個在您的線程仍在運行時被提前執行。併發是解決方案。

一種常見的Android的特定圖案的AsyncTask - http://developer.android.com/reference/android/os/AsyncTask.html - 或其類似物(線程/處理器等)

點擊後,啓動過程中onPreExecute(sendRequest將之前的代碼):比方說,彈出一個進度對話框並進行初始化。你的線程代碼進入doInBackground方法並設置狀態變量。完成後,在onPostExecute,駁回進度對話框,並與您的登錄邏輯進行:if(/*response*/ state!=null) ...

+0

謝謝。有效。 – ssrp

1

您應該使用的某些種類,將開始執行代碼時,如果你依賴返回值的線程完成一個回調。或者你可以啓動新的線程,並在做其他事情之前等待它返回。

相關問題