Last active
January 30, 2019 12:44
-
-
Save Razeeman/4142bd3b3a18936f5ad6b1e9e22d3a26 to your computer and use it in GitHub Desktop.
Android, Java, AsyncTask.
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 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