Skip to content

Instantly share code, notes, and snippets.

@huuchi207
Created April 21, 2019 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huuchi207/551bacc5f36af17c032d9f1b3426b669 to your computer and use it in GitHub Desktop.
Save huuchi207/551bacc5f36af17c032d9f1b3426b669 to your computer and use it in GitHub Desktop.
getData
private class GetData extends AsyncTask<String, Integer, String> {
public String convertIsToString(InputStream inputStream)
throws IOException{
StringBuilder builder = new StringBuilder();
BufferedReader reader =
new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
if (builder.length() == 0) {
return null;
}
return builder.toString();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog();
}
protected String doInBackground(String... urlString) {
HttpURLConnection conn= null;
InputStream is= null;
try {
URL requestURL = new URL(urlString[0]);
conn = (HttpURLConnection) requestURL.openConnection();
conn.setReadTimeout(20000 /* milliseconds */);
conn.setConnectTimeout(25000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
if (response != HttpURLConnection.HTTP_OK) {
return null;
}
is = conn.getInputStream();
String res = convertIsToString(is);
//close stream
conn.disconnect();
if (is != null) {
is.close();
}
//parse data and return
return res;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
protected void onPostExecute(String result) {
hideProgressDialog();
if (StringUtils.isEmpty(result)){
Toast.makeText(getApplicationContext(), "Empty result", Toast.LENGTH_LONG).show();
return;
}
if (result.contains("|")){
tvResult.setText(result);
final String[] mailString = result.split("\n");
Toast.makeText(getApplicationContext(), "Got "+ mailString.length + "mails from API", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
final Map<String, Object> map = new HashMap<>();
map.put(SettingKeys.URL_HOST_NAME, ViewUtils.getTrimmedText(etHostName));
map.put(SettingKeys.URL_TOKEN, ViewUtils.getTrimmedText(etToken));
map.put(SettingKeys.URL_AMOUNT, ViewUtils.getTrimmedText(etAmount));
map.put(SettingKeys.URL_TYPE, ViewUtils.getTrimmedText(etType));
Utils.checkAndCreateUserSettingsData(getApplicationContext(), new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
Utils.getCurrentUserSettingRef().update(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(getApplicationContext(), "Saved link", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Can not save link", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment