Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ucd.denver.com.diskussioner.entityParser; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; import ucd.denver.com.diskussioner.entity.Discussion; import ucd.denver.com.diskussioner.entity.User; /** * @author Chathura */ public class parseToJson { public static JSONObject parseRSToJson(ResultSet rs) { try { JSONObject jo = new JSONObject(); // Field[] cols = User.class.getDeclaredFields(); while (rs.next()) { jo.put("email", rs.getString(1)); jo.put("password", rs.getString(2)); jo.put("firstName", rs.getString(3)); jo.put("lastName", rs.getString(4)); // byte[] buffer = new byte[1]; InputStream is = rs.getBinaryStream(5); jo.put("photo", is); System.out.println("parseRSToJson++" + jo.toString()); } return jo; } catch (Exception e) { e.printStackTrace(); } return null; } public static User parseRSToUser(ResultSet rs) { System.out.println("rs rs rs" + rs); try { User u = new User(); System.out.println("reslut set::" + rs); while (rs.next()) { System.out.println(":::"); u.setEmail(rs.getString(1)); u.setPassword(rs.getString(2)); u.setFirstName(rs.getString(3)); u.setLastName(rs.getString(4)); u.setPhoto(rs.getBytes(5)); u.setJsonUser(parseUserToJASON(u)); System.out.println("::::::" + u.getJsonUser().toString()); } return u; } catch (Exception e) { e.printStackTrace(); return null; } } public static JSONObject parseUserToJASON(User user) { try { JSONObject jo = new JSONObject(); jo.put("email", user.getEmail()); jo.put("password", user.getPassword()); jo.put("firstName", user.getFirstName()); jo.put("lastName", user.getLastName()); // byte[] buffer = new byte[1]; if (user.getPhoto() != null) { InputStream is = new ByteArrayInputStream(user.getPhoto()); jo.put("photo", is); } else { jo.put("photo", "undifined"); } return jo; } catch (Exception e) { e.printStackTrace(); return null; } } public static User parceStringTOUser(String content) { try { JSONObject jo = new JSONObject(content); User u = new User(); if (jo.getString("status").equals("s")) { u.setEmail(jo.get("email").toString()); u.setPassword(jo.getString("password")); u.setFirstName(jo.getString("firstName")); u.setLastName(jo.getString("lastName")); System.out.println(" parceStringTOUser(String content) "); // String p = jo.get("photo").toString(); // // System.out.println(jo.get("photo").toString()); // System.out.println(p); // InputStream in =new ByteArrayInputStream(p.getBytes()); // System.out.println(in); // u.setInputStream(in); // // ByteArrayOutputStream buffer = new ByteArrayOutputStream(); // // int nRead; // byte[] data = new byte[1]; // // while ((nRead = in.read(data, 0, data.length)) != -1) { // buffer.write(data, 0, nRead); // } // // buffer.flush(); // u.setBytes(buffer.toByteArray()); // Bitmap bm = BitmapFactory.decodeStream(in); // u.setUserBitMap(bm); // in.reset(); // in.close(); System.out.println("END parceStringTOUser(String content) "); return u; } return null; } catch (Exception e) { e.printStackTrace(); return null; } } public static List<Discussion> parseToDiscussion(String content) { try { JSONArray ja = new JSONArray(content); List<Discussion> disList = new ArrayList<>(); for (int i = 0; i < ja.length(); i++) { JSONObject jo = ja.getJSONObject(i); Discussion d = new Discussion(); d.setId(jo.getInt("id")); String da = jo.getString("started_date").toString(); System.out.println(da); Date statedDate = new Date(); String[] dateSplit = da.split("-"); statedDate.setYear(Integer.parseInt(dateSplit[0]) - 1900); statedDate.setMonth(Integer.parseInt(dateSplit[1])); statedDate.setDate(Integer.parseInt(dateSplit[2])); System.out.println(statedDate); System.out.println("stated Date" + statedDate); d.setStartedDate(statedDate); d.setTopic(jo.getString("topic")); User u = new User(); u.setEmail(jo.getString("started_user")); d.setStartedUser(u); disList.add(d); } System.out.println("dis List size:::" + disList.size()); return disList; } catch (Exception e) { e.printStackTrace(); return null; } } public static Boolean parseEmailValidation(String content) { try { JSONObject jo = new JSONObject(content); boolean validity = jo.getBoolean("status"); return validity; } catch (Exception e) { e.printStackTrace(); return false; } } }