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

时间:2016-01-21 11:59:13

标签: android authentication http-post android-async-http loopj

我在项目中使用基于令牌的身份验证来控制登录身份验证。   我的基于令牌的身份验证需要:

   Header: Accept         Value: application/json
   Header: Content-Type   Value: application/x-www-form-urlencoded

   Key: grant_type        Value: password
   Key: username          Value: <NameToControl>
   Key: password          Value: <PasswordToControl>

我的基于令牌的身份验证网址:http://localhost:5978/GetAccessToken将是。

当我使用PostMan发布这些参数时,没有任何问题。但我无法在我的android应用程序中执行此操作。我在android应用程序上进行服务调用,但没有任何响应。我该如何解决?

我的静态客户端类:

public class PropClient {

    private static final String BASE_URL = "http://localhost:5978/";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(Context context,String url, StringEntity entity, AsyncHttpResponseHandler responseHandler) {
        client.addHeader("Accept", "application/json");
        client.addHeader("Content-Type", "application/x-www-form-urlencoded");
        entity.setContentType("application/json");
        client.post(context, getAbsoluteUrl(url), entity, "application/json", responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        String url = BASE_URL + relativeUrl;
        return url;
    }
}

我的主要活动类:

    JSONObject jsonParams = new JSONObject();
    jsonParams.put("grant_type", "password");
    jsonParams.put("username", mEmail);
    jsonParams.put("password", mPassword);
    StringEntity entity=null;
    try {
        entity = new StringEntity(jsonParams.toString());
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    PropClient.post(getBaseContext(), "GetAccessToken", entity, new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            // If the response is JSONObject instead of expected JSONArray
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
            // Pull out the first event on the public timeline
            // Do something with the response
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String response, Throwable e) {

        }

        @Override
        public void onRetry(int retryNo) {
            // called when request is retried
        }
    });

2 个答案:

答案 0 :(得分:0)

在yourStatic客户端类中尝试 private static final String BASE_URL =“http://xxx.xxx.x.xx:xxxx/”; 它会工作,然后检查你的IP地址,并把它放在localhost

的位置

答案 1 :(得分:0)

问题是您使用JSONObject来存储参数,并且inerpretation是错误的。 您必须发送如下字符串:

grant_type=password&username=demomail@example.it&password=Password1!

使用 RequestParams 并替换它:

JSONObject jsonParams = new JSONObject();
jsonParams.put("grant_type", "password");
jsonParams.put("username", mEmail);
jsonParams.put("password", mPassword);
StringEntity entity=null;
try {
    entity = new StringEntity(jsonParams.toString());
    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

使用:

RequestParams params = new RequestParams();
params.put("grant_type", "password");
params.put("username", mEmail);
params.put("password", mPassword);
StringEntity entity=null;
try {
    entity = new StringEntity(params.toString());
    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8"));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}