Android and JSON

This afternoon I needed to create a JSON string in Android. Trying to search this at google I found nearly no results, except for reading/parseing a JSON string. I didnt need that (yet), I just needed to serialize/create a JSON string. At the end I came up with the next class to generate a basic JSON string, and to use it to post to a website.

The class includes a method that returns a HttpEntity, that can be directly used to send to a website via HttpPost.

Class:

package paul.sohier.smstomail;

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * 
 * @author paul
 * 
 */
public class JSONCreate {
	/**
	 * Contains all Elements that will be exported to JSON
	 * 
	 * @access private
	 */
	private JSONObject elements;

	/**
	 * 
	 */
	public JSONCreate() {
		elements = new JSONObject();
	}

	/**
	 * Add a new element to a JSON string.
	 * 
	 * @param name String
	 *            JSON name field
	 * @param value String
	 *            JSON value field
	 * @return
	 */
	public boolean push(String name, String value) {

		try {
			elements.put(name, value);
		} catch (JSONException e) {
			Log.e("smstomail", "JSON writing error", e);
			return false;
		}
		return true;
	}

	/**
	 * Add a new element to a JSON string.
	 * @param name String JSON name field
	 * @param value JSONObject JSON object value
	 * @return
	 */
	public boolean push(String name, JSONObject value) {
		try {
			elements.put(name, value);
		} catch (JSONException e) {
			Log.e("smstomail", "JSON writing error", e);
			return false;
		}
		return true;
	}

	/**
	 * @return String string
	 */
	public String toString() {
		return elements.toString();
	}

	/**
	 * Create HttpEntity to be used for sending to remote host.
	 * 
	 * @return HttpEntity
	 */
	public HttpEntity getEntity() {
		StringEntity tmp;
		try {
			tmp = new StringEntity(elements.toString());
		} catch (UnsupportedEncodingException e) {
			Log.e("smstomail", "JSON writing exception", e);
			return null;
		}

		HttpEntity tmp2;
		tmp.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
				"application/json"));

		tmp2 = tmp;

		return tmp2;
	}
}

Usage example:

package paul.sohier.smstomail;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Connection {
	public boolean GetData() {
		JSONCreate j = new JSONCreate();
		j.push("data", "bla");
		
		HttpResponse tmp = postUrl(j, null);

		return false;
	}
	
	private HttpResponse postUrl(JSONCreate j, String url)
	{
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost request = new HttpPost(url);
	    request.setEntity(j.getEntity());
	    HttpResponse response = null;
	    try {
			response = httpclient.execute(request);
		} catch (ClientProtocolException e) {
			Log.e("smstomail", "HTTP client error", e);
		} catch (IOException e) {
			Log.e("smstomail", "HTTP client error", e);
		}
	    return response;		
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *