Android Post返回JSON字符串

时间:2014-03-16 21:49:42

标签: android ruby-on-rails http http-post

我正在设置rails api来处理我的Android应用程序的用户创建/身份验证。用户第一次登录应用程序时我想向创建用户的服务器发送一个帖子请求,然后检索用户的JSON并将他们的auth_token存储在我的应用程序中。

我能够发布/创建用户很好,但我正在努力检索JSON。我仍然是相当新的HTTP,所以请原谅我,如果这是一个简单的问题!我希望这很容易做到。

以下是我发布到我的网站以创建用户的方式:

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;
    }

这是我需要使用HttpURLConnection吗?我能够使用像这样回退JSON的GET请求:

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();
        }
    }

查看HttpURLConnection,它提供了通过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();
   }
 }

但是这似乎没有必要返回任何内容,而且我没有得到writeStream和readStream的方法存在错误。

任何示例,教程等都将非常感谢!如果我能够使用我的HttpPost检索JSON,那么这个例子绝对棒!

提前致谢!

编辑:

这是我的服务器日志:

» 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)

1 个答案:

答案 0 :(得分:1)

HttpPost应该足够了。试试这个以获得Json的回应:

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());
}