Android Open Source - 29onlinemultiplayer Json Parser






From Project

Back to project page 29onlinemultiplayer.

License

The source code is released under:

MIT License

If you think the Android project 29onlinemultiplayer 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 com.shaheed.online29;
//from ww w.  j a va  2  s .  c o m
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
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 Shaheed on 11/17/2014.
 */
public class JsonParser {
    InputStream inputStream = null;
    JSONObject jsonObject = null;
    String jsonS = null;

    public JSONObject makeHTTPRequest(String url,String method,List<NameValuePair> params){
        try{
            if(method.equals("POST")){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

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

                inputStream = httpEntity.getContent();

            }else if(method.equals("GET")){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params,"utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();

                inputStream = httpEntity.getContent();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine())!=null){
                stringBuilder.append(line+"\n");
            }
            inputStream.close();
            jsonS = stringBuilder.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
            jsonObject = new JSONObject(jsonS);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return jsonObject;
    }

}




Java Source Code List

.CreateGameServlet.java
.DatabaseHandler.java
.GameList.java
.JoinGameServlet.java
.LoginServlet.java
.RegistrationServlet.java
com.shaheed.online29.AccountMenuActivity.java
com.shaheed.online29.AccountMenu.java
com.shaheed.online29.ApplicationTest.java
com.shaheed.online29.Constants.java
com.shaheed.online29.GameActivity.java
com.shaheed.online29.GameJoinerActivity.java
com.shaheed.online29.GameListActivity.java
com.shaheed.online29.GameListAdapter.java
com.shaheed.online29.GameList.java
com.shaheed.online29.JsonParser.java
com.shaheed.online29.LoginActivity.java
com.shaheed.online29.RegistrationActivity.java
com.shaheed.online29.StartActivity.java
com.shaheed.online29.Variables.java