0

I am using Token Based Authentication on my project to control login authentication. My Token Based Authentication requires:

   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>

My Token Based Authentication URL: http://localhost:5978/GetAccessToken will be.

When I post these parameters with PostMan, there isn't any problem. But I cannot do it in my android application.I do service call on android applicaton, but there isn't any response. How can I fix it ?

My Static Client Class:

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

My MainActivity Class:

    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
        }
    });
fuat
  • 15
  • 5

2 Answers2

0

In yourStatic client class try private static final String BASE_URL = "http://xxx.xxx.x.xx:xxxx/"; it would work then check your ip address and put it in place of localhost

0

The problem is that you use a JSONObject to store the params, and the inerpretation is wrong. You must send a string like this:

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

Use RequestParams and replace this:

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

with:

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();
}
Dario Brux
  • 1,871
  • 1
  • 17
  • 15