Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class JSONfunctions {
- public static JSONArray getJSONfromURL(String url) {
- InputStream is = null;
- String result = "";
- JSONArray jArray = null;
- // Download JSON data from URL
- try {
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost(url);
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- } catch (Exception e) {
- Log.e("log_tag", "Error in http connection " + e.toString());
- }
- // Convert response to string
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- is, "iso-8859-1"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line + "\n");
- }
- is.close();
- result = sb.toString();
- } catch (Exception e) {
- Log.e("log_tag", "Error converting result " + e.toString());
- }
- try {
- jArray = new JSONArray(result);
- } catch (JSONException e) {
- Log.e("log_tag", "Error parsing data " + e.toString());
- }
- return jArray;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement