0

I am working in an android application that has to send user email through post method. The api endpoint is at https://api-iddog.idwall.co/ when i already run some tests and was able to GET values from here. Im using Loopj Library.

However, i've been tasked with using this endpoint:

https://api-iddog.idwall.co/signup

The documentation guide shows that i need to construct the POST method using the following parameters:

using Content-Type: application/json header

and the post URL should be something like:

Api Post Doc

With all that in mind, i got the create a method called attempPost(). On this method im creating the AsyncHttpRequest object, RequestParams object and storing the valid e-mail typed by the user.

private void attempPost() {

    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();

    String url = "https://api-iddog.idwall.co/signup";
    String userEmail = userEmailView.getText().toString();

    JSONObject jsonEmail = new JSONObject();
    try {
        jsonEmail.put("email", userEmail);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.d("Dog", " " + jsonEmail.toString());


    params.put("-d", jsonEmail);
    client.addHeader("Content-Type", "application/json");
    client.post(url, params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            Log.d("Dog", "onSucess" + responseBody.toString());

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            Log.e("Dog", "" + error.toString());

        }
    });



}

This method is called in a OnclickListener, and the log is returning a message called BadRequest and Status Code 400. I already tried to apply some code examples from Loopj doc but i didnt work. Can someone give me an insight in what i am doing wrong ? From what i can gather the success response should be a String(Token) and i need this token to complete the project.

EDIT.

I worked a little more through documentation and other examples here on Stack and i found this: POSTing JSON/XML using android-async-http (loopj)

In the accepted answer there is an example on how to post JSON.

Then i made a few changes in my attempPost method.

private void attempPost() {

    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();


    String url = "https://api-iddog.idwall.co/signup";
    String userEmail = userEmailView.getText().toString();

    JSONObject jsonParams = new JSONObject();
    try {
        jsonParams.put("email", "your@email.com");
    } catch (JSONException e) {
        e.printStackTrace();


    }

    client.post(getApplicationContext(), url, jsonParams, "application/json", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            Log.d("Dog", "onSucess" + responseBody.toString());

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            Log.e("Dog", "" + error.toString());
            Log.e("Dog", "status code " + String.valueOf(statusCode));
        }
    });

}

But Android Studio is telling me to convert jsonParams into a entity. In the example i got from stack its uses the following code to achieve that:

StringEntity entity = new StringEntity(jsonParams.toString());

However my android studio gets an horror that says:

Unhandled exception : java.io.UnsupportedEncodingException,

Until this moment i could not figure a way to solve this.

1 Answers1

0

I managed to make it work, the API its now returning a proper response and status code 200. I converted the JsonObject into a ByteArrayEntity since the StringEntity was not an option.

private void attempPost() {

    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams params = new RequestParams();

    String userEmail = userEmailView.getText().toString();

    JSONObject jsonParams = new JSONObject();
    try {
        jsonParams.put("email", userEmail);
    } catch (JSONException e) {
        e.printStackTrace();


    }

    ByteArrayEntity be = new ByteArrayEntity(jsonParams.toString().getBytes());


    //TODO create a class to handle JSON post, getmethods and parse token value
    client.post(getApplicationContext(), API_URL, be, "application/json", new JsonHttpResponseHandler() {


        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            Log.d("Dog", "onSucess " + response.toString());
            Log.d("Dog", "status code " + String.valueOf(statusCode));
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            Log.e("Dog", "" + errorResponse.toString());
            Log.e("Dog", "status code " + String.valueOf(statusCode));
        }


    });

}