Last active
August 29, 2015 14:17
-
-
Save IPRIT/48e5a0b2961875a96f03 to your computer and use it in GitHub Desktop.
HttpAsyncTask.fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HttpAsyncTask extends AsyncTask<URL, Integer, String> { | |
/** | |
* <URL, Integer, String> — URL::URL, Progress Report::Integer, Result::String | |
*/ | |
private final String LOG_TAG = TestAsyncTask.class.getSimpleName(); | |
public static final String POST_REQUEST_NAME = "POST"; | |
public static final String GET_REQUEST_NAME = "GET"; | |
public static final String HEAD_REQUEST_NAME = "HEAD"; | |
private String mSelectedMethod = POST_REQUEST_NAME; | |
public void setRequestMethod(String selected) { | |
if (!selected.equals(POST_REQUEST_NAME) && !selected.equals(GET_REQUEST_NAME) && | |
!selected.equals(HEAD_REQUEST_NAME)) { | |
return; | |
} | |
mSelectedMethod = selected; | |
} | |
@Override | |
protected void onPreExecute() { | |
// выполнение кода в UI потоке до запуска HTTP запроса | |
} | |
@Override | |
protected void onProgressUpdate(Integer... values) { | |
// Обновите индикатор хода выполнения, уведомления или другой | |
// элемент пользовательского интерфейса. | |
// Выполняется в UI потоке после каждого изменения | |
} | |
protected String doInBackground(URL... urls) { | |
// Выполняется не в UI потоке. Создается отдельный поток с помощью менеджера. | |
HttpURLConnection urlConnection = null; | |
BufferedReader reader = null; | |
String responseJsonStr = null; | |
try { | |
URL url = urls[0]; | |
// Create the request to MISIS Books, and open the connection | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setRequestMethod(mSelectedMethod); | |
urlConnection.connect(); | |
// Read the input stream into a String | |
InputStream inputStream = urlConnection.getInputStream(); | |
StringBuffer buffer = new StringBuffer(); | |
if (inputStream == null) { | |
// Nothing to do. | |
return null; | |
} | |
reader = new BufferedReader(new InputStreamReader(inputStream)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
buffer.append(line + "\n"); | |
} | |
if (buffer.length() == 0) { | |
// Stream was empty. No point in parsing. | |
return null; | |
} | |
return buffer.toString(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, "Error ", e); | |
// If the code didn't successfully get the misis books data, there's no point in attempting | |
// to parse it. | |
return null; | |
} finally { | |
if (urlConnection != null) { | |
urlConnection.disconnect(); | |
} | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (final IOException e) { | |
Log.e(LOG_TAG, "Error closing stream", e); | |
} | |
} | |
} | |
} | |
@Override | |
protected void onPostExecute(String response) { | |
Log.d(LOG_TAG, "[onPostExecute] Response has been received"); | |
// Сюда приходит ответ после завершения HTTP запроса | |
// Выполняется в UI потоке | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment