0
votes

I use loopj Android Asynchronous Http Client and I have problem with registration. Registration code:

    private void register(){

    RequestParams params = new RequestParams();
    params.put("email", "[email protected]");
    params.put("name", "Lurk");
    params.put("surname", "More");
    params.put("password", "123456789");
    params.put("person", "0");

    Client.post("register", params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            //Log.d("Json_reg", response.toString());

            try {
                JSONObject idJson = response.getJSONObject("_id");
                Log.d("Json_reg", response.toString());
                String strId = idJson.getString("$iod");
                Log.d("my_id", strId);
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            Log.d("THROW", throwable.toString());
        }
    });

}

The logs show me:

org.apache.http.client.HttpResponseException: Internal Server Error 07-28 11:14:48.676 20728-20728/com.example.murager.httpapp W/JsonHttpRH﹕ onFailure(int, Header[], Throwable, JSONObject) was not overriden, but callback was received org.apache.http.client.HttpResponseException: Internal Server Error

But when I Login everything is OK: Login code:

    private void login(){

    RequestParams params = new RequestParams();
    params.put("email", "[email protected]");
    params.put("password", "123456789");
    params.put("person", "0");

    Client.post("login", params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            try {

                Log.d("Json_con", response.toString());

                String nameStr = response.getString("name");
                String snameStr = response.getString("surname");

                JSONObject id = response.getJSONObject("_id");
                String strId = id.getString("$oid");

                String str = nameStr + "    " + snameStr + "    " + strId;
                tv.setText(str);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
}

My Client is here:

public class Client {

private static AsyncHttpClient client = new AsyncHttpClient();

private static String BASE_URL = "some_url";

public Client() {

}

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

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

}

My question is why to two similar functions show different result?

Registration with deprecated library work fine. Code here:

    private void makeRegistration(){

        String nameString = nameEditText.getText().toString();
        String surnameString = nameEditText.getText().toString();
        String emailString = emailEditText.getText().toString();
        String passwordString = passwordEditText.getText().toString();


        ArrayList<NameValuePair> param = new ArrayList<>();

        param.add(new BasicNameValuePair("name", nameString));
        param.add(new BasicNameValuePair("surname", surnameString));
        param.add(new BasicNameValuePair("email", emailString));
        param.add(new BasicNameValuePair("password", passwordString));
        param.add(new BasicNameValuePair("person", userTypeInt + ""));

        Log.d("User_type", userTypeInt + "");

        JSONParser jsonParser = new JSONParser();

        JSONObject jsonObject = jsonParser.makeHttpRequest("http://server/api/v1/r", "POST", param);

    Log.d("What_I_get_2", jsonObject.toString() + "\n");

    try {
        String name = jsonObject.getString("name");
        String surname = jsonObject.getString("surname");
        String id = jsonObject.getString("_id");

        Log.d("What_I_get", jsonObject.toString() + "\n" + id);

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
2

2 Answers

0
votes

org.apache.http.client.HttpResponseException: Internal Server Error means your server http://server_url/api/v1/ is throwing this error, Nothing wrong in your code.

If you have access to server logs, You should check it for more details, Otherwise ask respective server guy about issue. It might be the case you are missing some parameter in your register request. Anyways server should send back some meaningful http response code. 500 Internal server error is not correct.

0
votes

I have same error warning. Here is the trick:

Try to remove the line below from your overide method

super.onFailure(statusCode, headers, throwable, errorResponse);

It just works for me.