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