E / ClientProtocol: org.apache.http.client.HttpResponseException: внутренняя ошибка сервера - PullRequest
0 голосов
/ 09 октября 2019

открытый класс Вход в систему расширяет действие {

private final static String LOGIN_API_ENDPOINT_URL = "http://172.16.8.198:3000/api/v1/sessions";
private SharedPreferences mPreferences;
private String mUserEmail;
private String mUserPassword;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
}
public void login(View button) {
    EditText userEmailField = (EditText) findViewById(R.id.userEmail);
    mUserEmail = userEmailField.getText().toString();
    EditText userPasswordField = (EditText) findViewById(R.id.userPassword);
    mUserPassword = userPasswordField.getText().toString();

    if (mUserEmail.length() == 0 || mUserPassword.length() == 0) {
        // input fields are empty
        Toast.makeText(this, "Please complete all the fields",
                Toast.LENGTH_LONG).show();
        return;
    } else {
        LoginTask loginTask = new LoginTask(Login.this);
        loginTask.setMessageLoading("Logging in...");
        loginTask.execute(LOGIN_API_ENDPOINT_URL);
    }
}
    private class LoginTask extends UrlJsonAsyncTask {
        public LoginTask(Context context) {
            super(context);
        }

        @Override
        protected JSONObject doInBackground(String... urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(urls[0]);
            JSONObject holder = new JSONObject();
            JSONObject userObj = new JSONObject();
            String response = null;
            JSONObject json = new JSONObject();

            try {
                try {
                    // setup the returned values in case
                    // something goes wrong
                    json.put("success", false);
                    json.put("info", "Something went wrong. Retry!");
                    // add the user email and password to
                    // the params
                    userObj.put("email", mUserEmail);
                    userObj.put("password", mUserPassword);
                    holder.put("user", userObj);
                    StringEntity se = new StringEntity(holder.toString());
                    post.setEntity(se);

                    // setup the request headers
                    post.setHeader("Accept", "application/json");
                    post.setHeader("Content-Type", "application/json");

                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    response = client.execute(post, responseHandler);
                    json = new JSONObject(response);

                } catch (HttpResponseException e) {
                    e.printStackTrace();
                    Log.e("ClientProtocol", "" + e);
                    json.put("info", "Email and/or password are invalid. Retry!");
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("IO", "" + e);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("JSON", "" + e);
            }

            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            try {
                if (json.getBoolean("success")) {
                    // everything is ok
                    SharedPreferences.Editor editor = mPreferences.edit();
                    // save the returned auth_token into
                    // the SharedPreferences
                    editor.putString("AuthToken", json.getJSONObject("data").getString("auth_token"));
                    editor.commit();

                    // launch the HomeActivity and close this one
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                Toast.makeText(context, json.getString("info"), Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                // something went wrong: show a Toast
                // with the exception message
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
            } finally {
                super.onPostExecute(json);
            }
        }
    }
}

E / ClientProtocol: org.apache.http.client.HttpResponseException: внутренняя ошибка сервера W / System.err: org.apache.http.client.HttpResponseException: Внутренняя ошибка сервера в org.apache.http.impl.client.BasicResponseHandler.handleResponse (BasicResponseHandler.java:76) в org.apache.http.impl.client.BasicResponseHandler.handleResponse (BasicResponava.ache.63).http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java:662) в org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java:632) в org.apache.lienthlent.execute (AbstractHttpClient.java:621) на com.mkp.android.Login $ LoginTask.doInBackground (Login.java:90) на com.mkp.android.Login $ LoginTask.doInBackground (Login.java:57) на андроид. os.AsyncTask $ 2.call (AsyncTask.java:292) на java.util.concurrent.FutureTask.run (FutureTask.java:237) на android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:231 at),util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1115) в java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:590) в java.lang.Thread.run (Thread.java:818)ClientProtocol: org.apache.http.client.HttpResponseException: внутренняя ошибка сервера

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...