Skip to content Skip to sidebar Skip to footer

How Can I Use A Local Method Variable In A Totally Different Method?

I have a method written like this... public void getRequest(String Url) { runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub

Solution 1:

Increase the scope of variables of response and and request,I mean declare these variable at class level not in method level.

Solution 2:

You cannot call any variable declare as a local variable inside any function. you can do that in a way as follows

publicclassA{
    HttpGet request;
        HttpResponse response;
    voidmethodA(){
          request = //........
           response = //...........
    }
    void methodB{
        //here you can refer to request and response as they are the instance variables of the class.
    }
}

If you want to access those from outside the class, then you have to create an object of class A and then call as follows

Aa=newA();
//now you can call a.request or a.response

But remember the variables access specifiers should allow you to do this.

Solution 3:

I think what you're looking for is something along these lines:

protected Object myRequest;

publicvoidgetRequest(String Url) {
    runOnUiThread(newRunnable() {
        publicvoidrun() {
            HttpClientclient=newDefaultHttpClient();
            HttpGetrequest=newHttpGet(url);
            try {
                HttpResponseresponse= client.execute(request);
                myRequest = request(response);
                Toast.makeText(MenuUtama.this, myRequest, Toast.LENGTH_SHORT).show();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
}

Obviously change Object to whatever class request(response) works out to be, rename myRequest, and preferrably use accessors on a private instance variable rather than making it protected and assigning directly, but you get the point, hopefully, that you need an instance variable to hold the value of your method call request(response).

Solution 4:

you should declare your response variable in a class-level scope. here's your code.

publicclassYourClass{

    //Declare your request varible in a class-level scope//so it can be accessed by any method
    HttpGet request;


    publicvoidgetRequest(String Url) {

        runOnUiThread(new Runnable() {
            publicvoidrun() {
                 // TODO Auto-generated method stub

                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(url);
                try {
                    response = client.execute(request);
                    Toast.makeText(MenuUtama.this, request(response) ,Toast.LENGTH_SHORT).show();

                } catch (Exception ex) {
                ex.printStackTrace();
                }

            }
        });
    }

    publicvoidotherMthod(){
        System.out.println(request); //request variable is accessible in this scope.
    }
}

Post a Comment for "How Can I Use A Local Method Variable In A Totally Different Method?"