Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

api - cz.msebera.android.httpclient.client.HttpResponseException:

I face this problem and I don't know how to solve it here is my code: "cz.msebera.android.httpclient.client.HttpResponseException"

it going immediately to the OnFailure method: In both of the classes in sign in class and in sign up, I face the same error.

 signIn.setOnClickListener(v -> {

        //login onClickListener
        userString = email.getText().toString();
        passwordString = password.getText().toString();

        if(TextUtils.isEmpty(userString) || TextUtils.isEmpty(passwordString)){
            Toast.makeText(LoginPage.this,"Please fill email and password", Toast.LENGTH_LONG).show();

            return;
        }

        RequestParams params = new RequestParams();

        params.put("email", userString);
        params.put("password", passwordString);



        RestClient.post("/client/sign-in", params, new AsyncHttpResponseHandler() {

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


                try {
                    String res = new String(responseBody, "UTF-8");

                    JSONObject resObj = new JSONObject(res);


                    if (resObj.getString("sign-in") == "true") {

                        Toast.makeText(LoginPage.this, " Login Success", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(LoginPage.this, Profile.class);
                        startActivity(i);
                    }

                } catch (Exception ex) {
                    //Toast to catch


                    Toast.makeText(LoginPage.this, " Login Exception", Toast.LENGTH_SHORT).show();

                }

            }


            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(LoginPage.this , " Login Failure" , Toast.LENGTH_SHORT).show();

            }
        });
    });

and here is my connection class:

public class RestClient {

private final static String BASE_URL = "http://***.***.***.***:8081/**"; // the dns here

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(String url , RequestParams params , AsyncHttpResponseHandler responseHandler){

    client.setConnectTimeout(20000); //set the timeout in milliseconds
    client.setResponseTimeout(20000);
    client.setTimeout(20000);
    client.post(getAbsoluteURL(url) ,params,responseHandler);

}

private static String getAbsoluteURL (String relativeURL){return BASE_URL+relativeURL;}

}

I tried also without the Timeout and still the same problem I make sure of the spelling in the API I tested it in postman and it's working well


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...