/**
* Выполняет GET запрос к http-серверу,
* возвращает JSONArray, либо JSONObject */
public static Object getJsonFromServer(String relativeUrl) throws Exception {
URL url = new URL("http://example.com/" + relativeUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int statusCode = con.getResponseCode();
if (statusCode != HttpURLConnection.HTTP_OK)
throw new IOException(con.getResponseMessage() + " (HTTP " + statusCode + ")");
BufferedReader reader = new BufferedReader(new InputStreamReader(
con.getInputStream(), Charset.forName("utf-8")));
StringBuilder sb = new StringBuilder();
try {
String line;
while ((line = reader.readLine()) != null)
sb.append(line);
} finally {
reader.close();
}
String text = sb.toString();
if (TextUtils.isEmpty(text))
return null;
return new JSONArray("[" + text + "]").get(0);
}
This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.
Like popBackStack(String, int), but performs the operation immediately inside of the call. This is like calling executePendingTransactions() afterwards without forcing the start of postponed Transactions.