带有授权和获取请求的JSON帖子

时间:2017-11-18 10:33:49

标签: java android json post https

所以,大家好。我在连接服务器时遇到了一些问题。我使用过Yandex API和VK.COM API,我的方法还可以。但现在我无法从服务器上得到任何反响。我想,我做得不对。

所以网站是https://android-test.7g-demo.com 我也有API规范文件。以下是我对POST请求的看法。

# POST /oauth/token
  • 请求(application / json; charset = utf-8)

    • 接头

      Accept: application/json
      
    • {
          "username": "test@7glyphs.com",
          "password": "Test123",
          "grant_type": "password",
          "client_id": "2",
          "client_secret": "M1m2QgSdbRKWstTaVXlhdPotJ6WCC33rLLq3N6fK",
          "scope": "*"
      }
      
  • 响应200(application / json; charset = UTF-8)

    • {"token_type":"Bearer","expires_in":31536000,"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjkyYzAyYTE5ZGM2MjdmZDFhZTdkMzFiN2FkZTI1ODBlMWNmYTMxMjQxOWQzZmI3ZDNhYThmOGU0NmFjOWJlMGExOWI5YWI2NjI0ZmVjNmFmIn0.eyJhdWQiOiIyIiwianRpIjoiOTJjMDJhMTlkYzYyN2ZkMWFlN2QzMWI3YWRlMjU4MGUxY2ZhMzEyNDE5ZDNmYjdkM2FhOGY4ZTQ2YWM5YmUwYTE5YjlhYjY2MjRmZWM2YWYiLCJpYXQiOjE1MDk0ODgyODgsIm5iZiI6MTUwOTQ4ODI4OCwiZXhwIjoxNTQxMDI0Mjg4LCJzdWIiOiIxIiwic2NvcGVzIjpbIioiXX0.HrbgI2aYhqZCx_IwmvK7Y14HkOy2GzEL03o6Cwc7flqlb3Ijw2m-YFffEb5f9wX9Meo83iU3VSbdgx1oDpuJj5z3zOt0Q_QDJPn-seNG5Y0ob-BvAaMojUZFgbgLfL_xcatmW3TABKbC3NcUrgNym1Ru8qyBMvgT-ZY6iPlN13srWEH8qmMn0vYMB9-YJjfdz5mBHs-Y35APy4-6DdxiD_vKbppDBZCS1Nm_XULfoA-7MBVh9VOK2hngFxtERjiCZNol1FrljLO30gNwg2bD4WRZeFMH0o3ws3tkpDSwcbE2pQE8JokVXGZ46cJQConTmPhQ-yuPtCkggiwobs_QEMPLrTlkQSyIjLHm24xXlNQ3DLbw4AcDiOCDLYcrT8-Ef-9zYpDIxiYPnej4Fi4OHFanvxdwz-X8zqH8E8JKrdEPKPwMTE_Xx7xzazjyZKJY6y2GvVkU1BnvMa4VL5naRdYMp6TWnvo5hQiCBLpQXAbyH-U396aex2Nr5RctHdl2eM8KVhIHlofqMobUjeXwo-N4frpsAxjOj2uOco8zymFYmIirpjOCwyIkxiiF0lwzA6GOXzFlu2P6uzCpjvKTo2ID0pAT3wsA0Qf7lpDB0NjQlR3MAar6hHIncnDOYrndF8aX20i0fLDQvnpFZa43-z7GOXMO5f8ctTPO7q4Cuoc","refresh_token":"def50200d653d332230e5fbb8d094ba6fc99b6186739fdee26bd256171d07885ffe6afee1eb53247fe6a32c93dd31e53256d103b694007c5ec5ef8aadef0db64a7a4f46103182decb35bbfc2d39232b02649ebc1a32183a7cf1d498cf4aec14a4adde3ee8d708400811bdf4bf3ae4c067088ecd2ea1e8300e3c8a7a70e0900e62436a80c45069cb0552a067b230d74bce29e80233a71f47d0d9f670a26f7f660a1f81d52c2980a79b07e909df7871f1a9b39feb53a9f27e20599b5e66121c6866e91bf653fd78321ca6ad495e72ecb228386894375ec6ad5e200d8e9f1cc0df5d86b190214990ccbbe01bacc09114eebd70365f0d1f8a6932e2408deda013e160516d70b3aa209b7f5c9213ab58c3627dd700605a689ac8408d9e8f12bf3d21034441f22df1318d5a6cc011c776c92a41b6faec2497e3758f52c3bf288098db094a929ecfdf69a8dab6f693e97b7ba1afa1b5e25ab46e9b85314fc7ba78f6f5a60117d4e"}
      

但我不知道如何处理这一切。我写这段代码:

@Override
    protected Void doInBackground(Void... params) {
        try {
            URL url = new URL("https://android-test.7g-demo.com/?api.php");
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.connect();

            JSONObject jo = new JSONObject();

            jo.put("username", "test@7glyphs.com");
            jo.put("client_id", 2);
            jo.put("password", "Test123");

           BufferedOutputStream bos = new BufferedOutputStream(urlConnection.getOutputStream());
            bos.write(jo.toString().getBytes());

            String result = urlConnection.getResponseMessage();
            Log.d("", "server response: " + result);

        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }

        return null;
    }

但我没有服务器的回答。也许我应该把令牌放在任何地方或者也许应该使用任何库来进行oauth-request?或者我的代码看起来是另一种方式?谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

最好的方式和最简单的方法是使用改造而不是asyn任务 http://square.github.io/retrofit/