How can I use a local method variable in a totally

2019-09-20 15:49发布

I have a method written like this...

public void getRequest(String Url) {

runOnUiThread(new Runnable() {
    public void run() {
        // TODO Auto-generated method stub

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

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

    }
});
}

I need to be able to access the local variable request in another method, so that I can call request.response. How am I able to access this local method from a totally different method?

4条回答
Root(大扎)
2楼-- · 2019-09-20 16:17

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

protected Object myRequest;

public void getRequest(String Url) {
    runOnUiThread(new Runnable() {
        public void run() {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            try {
                HttpResponse response = 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).

查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-20 16:34

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

查看更多
倾城 Initia
4楼-- · 2019-09-20 16:37

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

public class A{
    HttpGet request;
        HttpResponse response;
    void methodA(){
          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

A a = new A();
//now you can call a.request or a.response

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

查看更多
看我几分像从前
5楼-- · 2019-09-20 16:37

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

public class YourClass{

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


    public void getRequest(String Url) {

        runOnUiThread(new Runnable() {
            public void run() {
                 // 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();
                }

            }
        });
    }

    public void otherMthod(){
        System.out.println(request); //request variable is accessible in this scope.
    }
}
查看更多
登录 后发表回答