2

I'm new to android programming and am working on a simple app, which will have to perfom POST and GET operations. I have a JSON parser class which creates a http request, but I keep getting an error on 'GET'. I have spent some time now trying to debug that and I have re-written the code a couple of times (I have also found similar threads on SO, but the suggested solutions didn't work for me), but I still cannot figure out what is wrong, as I keep getting the same error.

eee
  • 179
  • 1
  • 9

2 Answers2

3

you need to use .equals instead of == to compare Strings in Java so you need to convert:

if(method == "GET")

to:

if(method.equals("GET"))
Pooya
  • 6,083
  • 3
  • 23
  • 43
  • thanks! unfortunately this didn't fix the problem - I still get the same error :/ – eee Apr 21 '16 at 13:11
  • @eee how are you calling makeHttpRequest? – Pooya Apr 21 '16 at 13:13
  • @eee also in your switch(status) block, if there is another common error like (404, 500 , ...), then the json string will be "" which when you try to parse it, you will get an error – Pooya Apr 21 '16 at 13:17
  • Thanks, true, I didn't think about that, I've now deleted the switch statement, and moved the contents of it in another try/catch statement. I've edited the OP so it now contains the call of the method. – eee Apr 21 '16 at 13:40
  • @eee please update your makeHttpRequest to reflect your latest changes – Pooya Apr 21 '16 at 13:41
  • @eee you get the same error and exceptions? – Pooya Apr 21 '16 at 14:02
3

As Pooya have said you need to use ".equals".

Moreover, to avoid some null pointer you can invert the check

if("GET".equals(method))

Farolink
  • 57
  • 7