Reputation: 6080
I'm working on setting up a rails api to handle user creation/authentication for my android app. The first time the user logs into the app I want to send a post request to the server creating the user, then retrieve the user's JSON and store their auth_token in my app.
I'm able to post/create the user fine but I'm struggling with then retrieving the JSON. I'm still fairly new to HTTP so forgive me if this is an easy question! I'm hoping that it's fairly easy to do.
Here's how I've been posting to my site to create the user:
public static HttpResponse userRequest(String uri) {
try {
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
return new DefaultHttpClient().execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Is this something I need to use a HttpURLConnection for? I'm able to use a GET request that pulls back JSON like this:
static byte[] getUrlBytes(String urlSpec) throws IOException {
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return null;
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}
}
Looking at HttpURLConnection it gives this example of doing a post request via HttpURLConnection:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
But that doesn't seem to return anything necessarily and I get no method exist errors for writeStream and readStream respectively.
Any examples, tutorials, etc. would be much appreciated! And if I'm able to also retrieve the JSON with my HttpPost, an example of that would be absolutely awesome!
Thanks in advance!
EDIT:
Here's my server log:
» 19:07:39.856 2014-03-17 00:07:39.694240+00:00 app web.1 - - Redirected to http://localhost:3000/api/v1/appusers/28
» 19:07:39.856 2014-03-17 00:07:39.696476+00:00 app web.1 - - Completed 500 Internal Server Error in 13ms
» 19:16:56.582 2014-03-17 00:16:56.399291+00:00 heroku router - - at=info method=GET path=/api/v1/appusers/28 host=localhost:3000 request_id=edf4984e-77d3-4fd5-b5cc-bfde9d178586 fwd="75.100.35.119/h75-100-35-119.mdsnwi.dsl.dynamic.tds.net" dyno=web.1 connect=7ms service=13ms status=200 bytes=586
» 19:16:56.742 2014-03-17 00:16:56.392322+00:00 app web.1 - - Started GET "/api/v1/appusers/28" for 75.100.35.119/h75-100-35-119.mdsnwi.dsl.dynamic.tds.net at 2014-03-17 00:16:56 +0000
» 19:16:57.359 2014-03-17 00:16:56.393587+00:00 app web.1 - - Processing by Api::V1::AppusersController#show as JSON
» 19:16:57.359 2014-03-17 00:16:56.393587+00:00 app web.1 - - Parameters: {"id"=>"28"}
» 19:16:57.359 2014-03-17 00:16:56.397361+00:00 app web.1 - - Rendered api/v1/appusers/show.json.jbuilder (0.2ms)
Upvotes: 0
Views: 432
Reputation: 3907
HttpPost should suffice. Try this to get Json response:
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
DefaultHttpClient client=new DefaultHttpClient();
JSONObject response;
try {
String response_str = EntityUtils.toString(client.execute(httpPost).getEntity());
response = new JSONObject(response_str);
} catch (ParseException e) {
ADB.log("IOException: " + e.getMessage());
}
Upvotes: 1