Android Static functions

2.5k views Asked by At

I'm wondering how I can access the return statement with a static function. I have a static function with Async and I want to then get the return statement in another class - I know it sounds complex but, I'm sure it's an easy solution.

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;




        }
    }

Now, I want to see if ComHelper.SendLogin() returned true/or at least returned something.

EDIT: When the code is executed nothing happens, I guess that's because I'm not doing anything with the return statement.

2

There are 2 answers

32
Hunter McMillen On BEST ANSWER

If you want to look at the value, then you need to save the return value of the method in a local variable

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

You want to implement

protected void onPostExecute (Result result) 

on your AsyncTask implementation. The result parameter will be whatever you return from the doInBackground method. Since this runs in the UI thread you can modify the UI how you want at that time.