使用带有loopj库的AsyncHttpClient post请求使用基于令牌的身份验证

时间:2017-04-18 07:30:43

标签: android android-studio post loopj asynchttpclient

我在邮递员中得到的回复是相同的网址和令牌。我确信使用Post Request传递令牌存在问题。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这段代码可以帮助您发送带有令牌作为标题的请求。!

    public HttpResponse makeRequest(String uri, String json) {
        try {
            HttpClient httpclient = new DefaultHttpClient();


            HttpClientStack.HttpPatch httpPatch = new HttpClientStack.HttpPatch(uri); // create new httpGet object
            httpPatch.setHeader("Authorization", getAccessToken());

            httpPatch.setEntity(new StringEntity(json));
            httpPatch.setHeader("Content-Type", "application/json; charset=utf-8");

            HttpResponse response = httpclient.execute(httpPatch);
            HttpEntity entity = response.getEntity();
            resCoupon = EntityUtils.toString(entity);
            Log.d("requestride", resCoupon);

            return new DefaultHttpClient().execute(httpPatch);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;


    }