0

This question has been asked a lot, but most of the answers use the deprecated Android HTTPClient. Can someone help me rewrite the following GET request for Android/Java?

curl -u r0fls:12345 -i -X GET http://192.168.0.2/api/token

I was able to do a POST from Android to the server successfully, to create a new user, which I can show if that will be helpful. It took about 25 lines of Java though.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
rofls
  • 4,993
  • 3
  • 27
  • 37
  • 25 lines sounds about right for an `HttpURLConnection` implementation, assuming that is what you are using. If you got that `POST` working, what is it about this `GET` request that you do not understand? – CommonsWare May 09 '15 at 18:38
  • I tried simply switching `connection.setRequestMethod("POST");` to `connection.setRequestMethod("GET");` and the URL. The`POST` request that works is to create a new user, so it sends the same data as the one for a token (username/password). – rofls May 09 '15 at 18:41
  • ... but the cURL post format is a bit different. It specifies `'application/json'` content-type, which I did not remove or alter for the `GET` request. – rofls May 09 '15 at 18:42
  • It's still trying to do a `POST` request after I switch to `setRequestMethod('GET')`. – rofls May 09 '15 at 18:47
  • Then try removing it. For HTTP basic authentication, a Google search for `java httpurlconnection basic authentication example` turns up lots of hits, such as http://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java and http://stackoverflow.com/questions/7019997/preemptive-basic-auth-with-httpurlconnection. "It's still trying to do a POST request after I switch to setRequestMethod('GET')" -- consider editing your question and posting your code. – CommonsWare May 09 '15 at 18:47
  • thanks, I got it working using your links. – rofls May 09 '15 at 18:58

0 Answers0