Help us understand the problem. What is going on with this article?

AndroidでPOSTリクエストを送付する(HttpURLConnection利用)

More than 3 years have passed since last update.

以前、GETリクエストの記事を書きましたが
POSTリクエストも追記します。

送付するJSON文字列イメージ

{
    "user":{
    "name": "post",
    "password": "password",
    "password_confirmation": "password"
    }
}
final String json =
  "{\"user\":{" +
  "\"name\":\"name1\","+
  "\"password\":\"password\","+
  "\"password_confirmation\":\"password\""+
  "}}";

try {

   String buffer = "";
   HttpURLConnection con = null;
   URL url = new URL("URL");
   con = (HttpURLConnection) url.openConnection();
   con.setRequestMethod("POST");
   con.setInstanceFollowRedirects(false);
   con.setRequestProperty("Accept-Language", "jp");
   con.setDoOutput(true);
   con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
   OutputStream os = con.getOutputStream();
   PrintStream ps = new PrintStream(os);
   ps.print(json);
   ps.close();

   BufferedReader reader =
   new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
   buffer = reader.readLine();

   JSONArray jsonArray = new JSONArray(buffer);
   for (int i = 0; i < jsonArray.length(); i++) {
       JSONObject jsonObject = jsonArray.getJSONObject(i);
       Log.d("HTTP REQ", jsonObject.getString("name"));
   }
   con.disconnect();
   } catch (MalformedURLException e) {
       e.printStackTrace();
   } catch (ProtocolException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   } catch (Exception e) {
       e.printStackTrace();
   }
}

(参考)
JavaでHTTPリクエスト
http://www.magata.net/memo/index.php?Java%A4%C7HTTP%A5%EA%A5%AF%A5%A8%A5%B9%A5%C8

JavaでJSON文字列を扱うライブラリはたくさんありますが
それを入れるまでもない程度なので、文字列をJSONの書式にそって
作成してPOSTで投げています。

サーバサイドはrailsなんですが、JSONの書式に沿った文字列を
作るときちんと取得できません。。
たとえば、文字列の最外を角かっこにすると([])、うまく取得できない。
railsの動きを別途追ってみる予定です。

Java側では、JSON文字列に角かっこがないとnew JSONArray(文字列)で
JSONオブジェクトに変換失敗します。今はこれを基準にして統一しています。

(参考)
JavaScript Object Notation
https://ja.wikipedia.org/wiki/JavaScript_Object_Notation
今後、Javaの標準APIにJSONを扱う機能が追加されるようです。

kaikusakari
I'm noob.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした