Skip to content

Instantly share code, notes, and snippets.

@Razeeman
Last active January 30, 2019 12:44
Show Gist options
  • Save Razeeman/4142bd3b3a18936f5ad6b1e9e22d3a26 to your computer and use it in GitHub Desktop.
Save Razeeman/4142bd3b3a18936f5ad6b1e9e22d3a26 to your computer and use it in GitHub Desktop.
Android, Java, AsyncTask.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
query = "sample query"
URL searchUrl = NetworkUtils.buildUrl(query);
new QueryTask().execute(searchUrl);
}
public class QueryTask extends AsyncTask<URL, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(URL... params) {
URL searchUrl = params[0];
String searchResults = null;
try {
searchResults = NetworkUtils.getResponseFromHttpUrl(searchUrl);
} catch (IOException e) {
e.printStackTrace();
}
return searchResults;
}
@Override
protected void onPostExecute(String searchResults) {
if (searchResults != null && !searchResults.equals("")) {
showData(searchResults);
} else {
showErrorMessage();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment