Как правильно послать запрос и получить данные?

276
28 июня 2018, 08:30

Имеются два класса, полностью одинаковые, но с одним отличием, первый класс не выдает результат, то есть ни ошибки ни результата нет. В логе пишет что NullPointerException, но приложение не вылетает, а просто возвращает нулевой результат. При всем при этом если взять уже сформированную ссылку например:

http://md5decrypt.net/en/Api/api.php?hash=5f4dcc3b5aa765d61d8327deb882cf99&hash_type=md5&email=ser47400@gmail.com&code=68fdce9898fb7239

То результат выдает корректно и без ошибок. В связи с этим вопрос в чем моя ошибка?

P. S. не претендую на лучший синтаксис, приложение просто для теста.

Первый класс

class Connection extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... strings) {

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        String result_hash = "";
        String hash = editText.getText().toString();
        String link = "&hash_type=md5&email=ser47400@gmail.com&code=68fdce9898fb7239";
        String all = hash + link;

        try {
            URL url = new URL("http://md5decrypt.net/en/Api/api.php?hash=" + all);
            urlConnection = (HttpURLConnection) url.openConnection();
            reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            result_hash = reader.readLine();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result_hash;
    }

    @Override
    protected void onPostExecute(String result) {
        editText.setText(result);
    }
}
class DescRypted extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        String result_hash = "";
        String hash = editText.getText().toString();
        String link = "&hash_type=md5&email=ser47400@gmail.com&code=68fdce9898fb7239";
        String all = hash + link;

        try {
            URL url = new URL("http://md5decrypt.net/en/Api/api.php?word=" + all);
            urlConnection = (HttpURLConnection) url.openConnection();
            reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            result_hash = reader.readLine();
            String LOG_TAG = "result";
            Log.d(LOG_TAG, result_hash);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result_hash;
    }
    @Override
    protected void onPostExecute(String result) {
        editText.setText(result);
    }
}
READ ALSO
Как распарсить JSON и вывести данные в RecycleView?

Как распарсить JSON и вывести данные в RecycleView?

как правильно парсить такой jsonнадо получить следующие данные и выводить в recycleview

328
Dependencies в Maven [дубликат]

Dependencies в Maven [дубликат]

На данный вопрос уже ответили:

221
Отправка PUSH-уведомлений на устройства Apple: java.lang.IllegalStateException: Channel closed before HTTP/2 preface completed

Отправка PUSH-уведомлений на устройства Apple: java.lang.IllegalStateException: Channel closed before HTTP/2 preface completed

Пытаюсь отправить push-уведомления на устройства AppleИспользую библиотеку:

223
Свой Behavior для View внутри Toolbar в CoordinatorLayout

Свой Behavior для View внутри Toolbar в CoordinatorLayout

Недавно начал изучать CoordinatorLayout и решил написать свой Behavior

211