2012-09-21 24 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

您应该使用的某些种类,将开始执行代码时,如果你依赖返回值的线程完成一个回调。或者你可以启动新的线程,并在做其他事情之前等待它返回。

相关问题