Android静态功能

时间:2012-06-04 14:54:09

标签: java android function static-functions

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

Login.class

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语句做任何事情。

2 个答案:

答案 0 :(得分:1)

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

if(user1 !=null && user1.length() > 0 && pass1 !=null && pass1.length() > 0) 
{
     boolean comLogin = ComHelper.SendLogin(user1, pass1);
     if(comLogin)
     {
         //do something
     }
}

答案 1 :(得分:1)

您想要实施

protected void onPostExecute (Result result) 

关于AsyncTask实现。 result参数将是您从doInBackground方法返回的任何内容。由于这在UI线程中运行,因此您可以在此时修改UI。