2012-07-18 14 views
0

遺憾的是一種痛苦,但我一直在這一個時間過長,我相信這是一個簡單的人,但我累了,不能看它。所有工作正常,但在「字符串結果」爲空Android的Eclipse中的AsyncTask - 但結果是空的 - 有人可以看看

package com.example.me; 

import java.util.ArrayList; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.widget.Button; 
import android.widget.TextView; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MainActivity extends Activity { 

    Button btnLoginButton; 
    TextView tmpError, tmpUsername, tmpPassword; 
    ArrayList<NameValuePair> postParameters; 
    String response; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tmpError = (TextView) findViewById(R.id.lblMessage); 
     tmpUsername = (TextView) findViewById(R.id.txtUsername); 
     tmpPassword = (TextView) findViewById(R.id.txtPassword); 

     addListenerOnButton(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void addListenerOnButton() { 

     btnLoginButton = (Button) findViewById(R.id.btnLogin); 
     btnLoginButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg) { 
       try{ 
        triggerClick(); 
       } 
       catch (Exception e) {   
        tmpError.setText("[]" + e.toString()); 
       } 
      } 
     }); 
    } 

    private void triggerClick() { 

     postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("username", tmpUsername.getText().toString())); 
     postParameters.add(new BasicNameValuePair("password", tmpPassword.getText().toString())); 

     final class HttpTask 
       extends 
       AsyncTask<String/* Param */, Boolean /* Progress */, String /* Result */> { 

      @Override 
      protected String doInBackground(String... params) { 
       publishProgress(true); 
       try { 
       response = CustomHttpClient.executeHttpPost("http://some.url/thatiknoworks/check.php", postParameters); 
          return response; 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }  

      } 

      @Override 
      protected void onPostExecute(String result) { 
       publishProgress(false); 

       result = result.replaceAll("\\s+",""); 

       if(result.equals("1")) { 
        tmpError.setText("Correct"); 
       } 
       else { 
        tmpError.setText("Sorry!!("+result+")"); 
       } 
      } 

     }  

     new HttpTask().execute(); 
    } 
} 

因爲在doInBackground()返回空字符串,你應該做一個空的「結果」的字符串:-(

+0

請檢查我的答案。 – 2012-07-18 12:31:12

+0

是鮑勃迪倫它以 「迴歸CustomHttpClient.executeHttpPost(」 HTTP://some.url/thatiknoworks/check.php 「postParameters);」做過 – conners 2012-07-18 12:32:13

+0

好吧然後很好。但是你可以使用我在我的回答中建議的方式由於服務器的響應將以字符串的形式,並且可以在全局範圍內保存該字符串,並使用其他位置。 – 2012-07-18 12:34:54

回答

1

回來一次又一次:

protected String doInBackground(String... params) { 
      publishProgress(true); 
      try { 
       return CustomHttpClient.executeHttpPost("http://some.url/thatiknoworks/check.php", postParameters); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       return ""; 
      }  

     } 
+0

我明白你的意思,但我不認爲這是它,我忘了提及的是,HTTP://some.url/thatiknoworks/check.php返回無論是「1」(真)或「 0" (假),它的這個價值,我想的AsyncTask給我..所以我真的不希望在doInBackground()我想onPostExecute(字符串結果)返回任何有httpsrequest結果它 – conners 2012-07-18 12:28:26

+0

確定,如果我返回CustomHttpClient.executeHttpPost(「http://some.url/thatiknoworks/check.php」,postParameters); 它似乎工作正常:D但 – conners 2012-07-18 12:31:00

+0

雅是這樣的,你應該返回的CustomHttpClient.executeHttpPost()方法的迴應 – Nermeen 2012-07-18 12:32:14

1

字符串的結果,因爲你是從返回的doInBackground()空字符串爲空

return ""; 
1

請聲明String response;作爲一個全局變量。

protected String doInBackground(String... params) 
{ 
    publishProgress(true); 
    try 
    { 
    response=CustomHttpClient.executeHttpPost("http://some.url/thatiknoworks/check.php", postParameters); 
    return response; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    }  
} 
+0

I '我試圖得到分數 – conners 2012-07-18 12:41:40

+0

好吧..沒關係,當它解決了你的問題.. :) – 2012-07-18 12:45:45

相關問題