Android : Parse JSON data with several data types into database table from Android app?

on Monday, November 10, 2014


I have created a data object model:



package com.example.apex.apex ;

import java.sql.Date ;

public class Cyclist {

enum Gender {

Male, Female
}

enum Location {

Antrim, Armagh, Carlow, Cavan, Clare,
Cork, Derry, Donegal, Down, Dublin,
Fermanagh, Galway, Kerry, Kildare,
Kilkenny, Laois, Leitrim, Limerick,
Longford, Louth, Mayo, Meath, Monaghan,
Offaly, Roscommon, Sligo, Tipperary,
Tyrone, Waterford, Westmeath, Wexford,
Wicklow
}

String firstName;
String lastName;
String emailAddress;
String password;
Location county;
Date birthDate;
Gender gender;
int heightCm;
double weightKg;

// constructor
public Cyclist() {

}

public Cyclist(String fName, String lName, String email,
String password, Location county, Date dob,
Gender gender, int height, double weight) {

this.firstName = fName;
this.lastName = lName;
this.emailAddress = email;
this.password = password;
this.county = county;
this.birthDate = dob;
this.gender = gender;
this.heightCm = height;
this.weightKg = weight;
}

@Override
public String toString() {
return String.format("First Name: " + this.firstName + ", Last Name: " + this.lastName
+ ", Email Address: " + this.emailAddress + ", Password: " + this.password
+ ", County: " + this.birthDate + ", Gender: " + this.gender + ", Height: "
+ heightCm + ", Weight: " + weightKg);
}

}


I also have a JSONParser class:



public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

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

// 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, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", 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;

}
}


CyclistFunctions.java



package com.example.apex.apex ;

public class CyclistFunctions {

private JSONParser jsonParser;

private static final String registerURL = "http://127.0.0.1/apexdb/include/db_functions.php/";
private static final String loginURL = "http://127.0.0.1/apexdb/include/db_functions.php/";

private static String register_tag = "register";
private static String login_tag = "login";

// constructor
public CyclistFunctions() {
jsonParser = new JSONParser();
}

/** public JSONObject loginCyclist(String email_address, String password) { // build parameters
* List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("tag",
* login_tag)); params.add(new BasicNameValuePair("email_address", email_address)); params.add(new
* BasicNameValuePair("password", password)); JSONObject json = jsonParser.getJSONFromUrl(loginURL, method,
* params); }
* <p>
* /**
* make register request
*/
public JSONObject registerUser(String fName, String lName, String bio) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("first_name", fName));
params.add(new BasicNameValuePair("last_name", lName));
//etc

JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);

return json;
}

}


If my database table consisted only of string values it would be fine as I would parse my data directly to JSON, however as I need to convert multiple data types, how might I go about this? I looked at Gson, however it was not very clear to me.


0 comments:

Post a Comment