0

I have to declare a variable, the type of this variable depends on a parameter, so I have an if clause and define this variable there:

if(this.httpMethod == "POST"){
    HttpPost request = new HttpPost(url);
}
if(this.httpMethod == "GET"){
    HttpGet request = new HttpGet(url);
}

The problem is that in Java, the scope of that variable lies inside the if, so I can't use it later !. If I try:

request.addHeader("Accept", "application/json");

That gives me: request cannot be resolved

So, the question would be: how can I define a variable, that it's type depends on a parameter.

What I have tried:

  • Setting the variable to final.

My current solution is to duplicate the code, inside every if clause, I set the same Headers, Connection, grab the response, .... So, of course, I don't like this.

Design. I have this HTTP request class like this, so I can use it to handle the request, so, I just have to send it some parameters (URL, method, authentication, ...) , and it will give me back the result. This class is my bottom layer of my Android app, that looks like this:

  1. Activities.java (all the activities)
  2. Core.java (handle resources)
  3. HttpRequest.java (handle internet resources)
2
  • 2
    if(this.httpMethod.equals("POST")). use .equals to compare strigns Commented May 19, 2014 at 17:31
  • @Raghunandan Thanks for remind me that. I will change it. Although my question still open... Commented May 19, 2014 at 17:40

3 Answers 3

1

Both HttpPost and HttpGet inherit HttpRequestBase. So you can declare the request variable outside the if statement like so:

HttpRequestBase request = null;

if (this.httpMethod.equals("POST")) {
   request = new HttpPost(url);
}
else if (this.httpMethod.equals("GET")) {
   request = new HttpGet(url);
}

request.addHeader(...); etc
Sign up to request clarification or add additional context in comments.

1 Comment

I just tested this. Works perfect. Thanks for the explanation also.
0

You should declare a variable of both variable types' superclass outside the if statements. Example:

Superclass object;

if (condition){
    object = new Subclass1();
}
else {
    object = new Subclass2();
}

1 Comment

Thank you, I think you meant like this: stackoverflow.com/a/23743896/2615737
0

You need to declare the variable to be of the supertype outside of the if blocks and then use it inside them:

        HttpRequestBase request = null;

        if("POST".equals(this.httpMethod)){
            request = new HttpPost(url);
        }
        else if("GET".equals(this.httpMethod)){
            request = new HttpGet(url);
        }

And then later in your code when you're ready to use it, be sure to check whether the request is null or not.

Edited based on changes to the original question

5 Comments

I can't, Java won't let me change the type of a variable.
This sidesteps the important issue, though, because in the code you're skipping the difference between the two methods.
I just realized you had a typo in your posted code and were setting two different subtypes.
I edited my question. Yes, that answer work. Thank you. stackoverflow.com/a/23743896/2615737
Edited my answer to be correct as well. Please note that the "GET".equals(this.httpMethod) style will not throw a NullPointerException if httpMethod is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.