0

我想知道如何使用静态函数访问 return 语句。我有一个带有 Async 的静态函数,然后我想在另一个类中获取 return 语句 - 我知道这听起来很复杂,但我确信这是一个简单的解决方案。

登录类

public class LogIn extends Activity {
    Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        TextView top = (TextView) findViewById(R.id.textView2);
        final EditText user = (EditText) findViewById(R.id.etUser);
        final EditText pass = (EditText) findViewById(R.id.etPass);
        CheckBox stay = (CheckBox) findViewById(R.id.cBStay);
        Button login = (Button) findViewById(R.id.btLogin);





    login.setOnClickListener( new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String user1 = user.getText().toString();
             String pass1 = pass.getText().toString();
            if(user1 !=null &user1.length()>=1 & pass1 !=null &pass1.length()>=1) {
                ComHelper.SendLogin(user1, pass1);

            }
        }
    });


    }



}

ComHelper.class

public class ComHelper extends AsyncTask<String, Void, String> {
    static String adress ="http://gta5news.com/login.php";
    String user;
    String pass;
    public static boolean SendLogin(String user1, String pass1){
    String user = user1.toString();
    String pass = pass1.toString();
    new ComHelper().execute(user1, pass1, adress);
    return true;

    }


    private static StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;

    }



    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        InputStream inputStream = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost post = new HttpPost(adress);
        try {
            /*Add some data with NameValuePairs */
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("user", user));
            nameValuePairs.add(new BasicNameValuePair("password", pass));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            /*Execute */
            HttpResponse response = httpclient.execute(post);
            String str = inputStreamToString(response.getEntity().getContent())
                    .toString();
            Log.w("HttpPost", str);

            if (str.toString().equalsIgnoreCase("true"))
                return str;

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

        }



        return null;




        }
    }

现在,我想看看是否ComHelper.SendLogin()返回了 true/或至少返回了一些东西。

编辑:当代码执行时什么都没有发生,我想那是因为我没有对 return 语句做任何事情。

4

2 回答 2

1

你想实施

protected void onPostExecute (Result result) 

在您的 AsyncTask 实现上。结果参数将是您从 doInBackground 方法返回的任何值。由于它在 UI 线程中运行,因此您可以在那时按照您的需要修改 UI。

于 2012-06-06T19:35:18.233 回答
0

如果要查看值,则需要将方法的返回值保存在局部变量中

if(user1 !=null && user1.length() > 0 && pass1 !=null && pass1.length() > 0) 
{
     boolean comLogin = ComHelper.SendLogin(user1, pass1);
     if(comLogin)
     {
         //do something
     }
}
于 2012-06-04T14:57:16.927 回答