Android Open Source - FootyNews J S O N Parser






From Project

Back to project page FootyNews.

License

The source code is released under:

MIT License

If you think the Android project FootyNews listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package taz.starz.footynews.library;
/*w  ww.  j  av a2s. com*/
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Thahzan on 6/26/2014.
 */
public class JSONParser {

    public JSONParser() {
        // Empty constructor
    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        InputStream is = null;
        JSONObject jObj = new JSONObject();
        JSONArray jArr = null;
        String json = "";

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.d("JSON Response", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

    public JSONArray getJSONArrayFromUrl(String url, List<NameValuePair> params) {

        InputStream is = null;
        JSONArray jArr = new JSONArray();
        String json = "";

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.d("JSON Response", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jArr = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jArr;

    }

}




Java Source Code List

com.nirhart.parallaxscroll.BuildConfig.java
com.nirhart.parallaxscroll.BuildConfig.java
com.nirhart.parallaxscroll.views.ParallaxExpandableListView.java
com.nirhart.parallaxscroll.views.ParallaxListViewHelper.java
com.nirhart.parallaxscroll.views.ParallaxListView.java
com.nirhart.parallaxscroll.views.ParallaxScrollView.java
com.nirhart.parallaxscroll.views.ParallaxedView.java
org.arasthel.googlenavdrawermenu.adapters.GoogleNavigationDrawerAdapter.java
org.arasthel.googlenavdrawermenu.utils.Utils.java
org.arasthel.googlenavdrawermenu.views.CheckableImageView.java
org.arasthel.googlenavdrawermenu.views.CheckableRelativeLayout.java
org.arasthel.googlenavdrawermenu.views.CheckedTextView.java
org.arasthel.googlenavdrawermenu.views.GoogleNavigationDrawer.java
shared.ui.actionscontentview.ActionsContentView.java
shared.ui.actionscontentview.ActionsLayout.java
shared.ui.actionscontentview.BaseContainerController.java
shared.ui.actionscontentview.BuildConfig.java
shared.ui.actionscontentview.ContainerController.java
shared.ui.actionscontentview.ContentLayout.java
shared.ui.actionscontentview.EffectsController.java
taz.starz.footynews.MainActivity.java
taz.starz.footynews.OfflineReadActivity.java
taz.starz.footynews.SplashScreen.java
taz.starz.footynews.ViewOfflineActivity.java
taz.starz.footynews.ViewStoryActivity.java
taz.starz.footynews.adapter.CustomListAdapter.java
taz.starz.footynews.adapter.CustomOfflineReaderAdapter.java
taz.starz.footynews.fragments.BundesLigaFragment.java
taz.starz.footynews.fragments.LigaBBVAFragment.java
taz.starz.footynews.fragments.PremierLeagueFragment.java
taz.starz.footynews.fragments.SerieAFragment.java
taz.starz.footynews.library.DatabaseHandler.java
taz.starz.footynews.library.JSONParser.java
taz.starz.footynews.library.NetworkFunctions.java
taz.starz.footynews.library.NewsItem.java
taz.starz.footynews.library.SharedPref.java