问题
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?
回答1:
Increase the scope of variables of response and and request,I mean declare these variable at class level not in method level.
回答2:
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.
回答3:
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)
.
回答4:
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.
}
}
来源:https://stackoverflow.com/questions/9919347/how-can-i-use-a-local-method-variable-in-a-totally-different-method