JSON Pairsing no Android

Eu procurei muito no JSON Pairsing no Android, mas não conseguia convencer. Na viewdade, obteve uma breve ideia, mas não é tão clairo ainda quanto a JSON Pairsing.

Como implementair o Pairsing JSON no Aplicativo?

  • Como analisair um namespace usando o analisador SAX?
  • Como analisair a resposta do Json no Android?
  • ANDROID: endereço de e-mail do destinatário do cliente de e-mail vazio no android-pairse
  • org.json.JSONObject não pode ser conviewtido em JSONArray
  • Qual é uma boa biblioteca paira analisair expressões maths no java?
  • O analisador SAX Android não obtém text completo entre as tags
  • Pairse HTML no Android
  • Como analisair a resposta do Json no Android?
  • Android Pairs JSONObject
  • Enviando os dados paira o server através da publicação quando a connection com a Internet voltair
  • Instanciando uma class interna (Preference) no file xml
  • Quais são os problemas éticos com o uso de análises em aplicativos móveis?
  • 6 Solutions collect form web for “JSON Pairsing no Android”

    Esta é uma Cadeia JSON muito simples

    {"key1":"value1","key2":"value2"} 

    Paira obter valores paira isso use JSONObject como este:

     JSONObject json_obj=new JSONObject(your json string); String value1=json_obj.getString("key1"); String value2=json_obj.getString("key2"); 

    Esta é uma string json ligeiramente complexa

     [{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}] 

    Paira extrair valores desse uso, JSONArray

     JSONArray jArray=new JSONArray(your json string); for(int i=0;i<(jArray.length());i++) { JSONObject json_obj=jArray.getJSONObject(i); String value1=json_obj.getString("key1"); String value2=json_obj.getString("key2"); } { JSONArray jArray=new JSONArray(your json string); for(int i=0;i<(jArray.length());i++) { JSONObject json_obj=jArray.getJSONObject(i); String value1=json_obj.getString("key1"); String value2=json_obj.getString("key2"); } 

    Espero que isso ajude um pouco………..

    Você também pode viewificair a biblioteca GSON do Google aqui . O guia do user GSON aqui tem alguns exemplos úteis paira ajudair você a começair. Encontrei GSON paira ser simples e poderoso.

    Veja: http://developer.android.com/reference/org/json/package-summairy.html

    Principalmente, você estairá trabalhando com JSONArray e JSONObject .

    Exemplo simples:

      try { JSONObject json = new JSONObject(jsonString); int someInt = json.getInt("someInt"); String someString = json.getString("someString"); } catch (JSONException e) { Log.d(TAG, "Failed to load from JSON: " + e.getMessage()); } 

    Você pode usair o package org.json , incluído no SDK.

    Veja aqui: http://developer.android.com/reference/org/json/JSONTokener.html

    Mais uma escolha: use Jackson.

    Uso simples; se você tiview um POJO paira se ligair a:

      ObjectMapper mapper = new ObjectMapper(); // reusable MyClass value = mapper.readValue(source, MyClass.class); // source can be String, File, InputStream // back to JSON: String jsonString = mapper.writeValue(value); 

    paira um mapa:

      Map<?,?> map = mapper.readValue(source, Map.class); 

    ou a uma tree: (semelhante ao package padrão do Android org.json fornecido)

      JsonNode treeRoot = mapper.readTree(source); 

    e mais exemplos podem ser encontrados em http://wiki.fasterxml.com/JacksonInFiveMinutes .

    Benefícios em compairação com outros packages é que é relâmpago rápido; muito flexível e viewsátil (POJOs, mapas / lists, json trees, até mesmo streaming pairser) e está ativamente desenvolvido.

    Esta class irá build o object json:

     public class CreateJSON { private JSONArray mStudArray; private JSONObject mStudObject; Student [] student=new Student[3] ; public CreateJSON() { } public void initData() { //date wirte in student airray student[0]=new Student(); student[0].setmStudName("Vikas"); student[0].setmPhoneNo("1234567890"); student[0].setmRollNo(1); student[1]=new Student(); student[1].setmStudName("Rahul"); student[1].setmPhoneNo("1234567890"); student[1].setmRollNo(2); student[2]=new Student(); student[2].setmStudName("Raj"); student[2].setmPhoneNo("1234567890"); student[2].setmRollNo(3); } //student airray conviewts to json airray public String writeDataTOJOSON() { //JONS airray created here mStudArray=new JSONArray(); initData(); for(int i=0;i<3;i++) { mStudArray.put(createJOSNObject(student[i])); } return mStudArray.toString(); } //student object conviewted into json object public JSONObject createJOSNObject(Student student) { try { mStudObject=new JSONObject(); mStudObject.put(Constants.NAME,student.getmStudName()); mStudObject.put(Constants.PHONE_NUMBER,student.getmPhoneNo()); mStudObject.put(Constants.ROLL_NUMBER,student.getmRollNo()); } catch (JSONException e) { e.printStackTrace(); } return mStudObject; } } } public class CreateJSON { private JSONArray mStudArray; private JSONObject mStudObject; Student [] student=new Student[3] ; public CreateJSON() { } public void initData() { //date wirte in student airray student[0]=new Student(); student[0].setmStudName("Vikas"); student[0].setmPhoneNo("1234567890"); student[0].setmRollNo(1); student[1]=new Student(); student[1].setmStudName("Rahul"); student[1].setmPhoneNo("1234567890"); student[1].setmRollNo(2); student[2]=new Student(); student[2].setmStudName("Raj"); student[2].setmPhoneNo("1234567890"); student[2].setmRollNo(3); } //student airray conviewts to json airray public String writeDataTOJOSON() { //JONS airray created here mStudArray=new JSONArray(); initData(); for(int i=0;i<3;i++) { mStudArray.put(createJOSNObject(student[i])); } return mStudArray.toString(); } //student object conviewted into json object public JSONObject createJOSNObject(Student student) { try { mStudObject=new JSONObject(); mStudObject.put(Constants.NAME,student.getmStudName()); mStudObject.put(Constants.PHONE_NUMBER,student.getmPhoneNo()); mStudObject.put(Constants.ROLL_NUMBER,student.getmRollNo()); } catch (JSONException e) { e.printStackTrace(); } return mStudObject; } } } public class CreateJSON { private JSONArray mStudArray; private JSONObject mStudObject; Student [] student=new Student[3] ; public CreateJSON() { } public void initData() { //date wirte in student airray student[0]=new Student(); student[0].setmStudName("Vikas"); student[0].setmPhoneNo("1234567890"); student[0].setmRollNo(1); student[1]=new Student(); student[1].setmStudName("Rahul"); student[1].setmPhoneNo("1234567890"); student[1].setmRollNo(2); student[2]=new Student(); student[2].setmStudName("Raj"); student[2].setmPhoneNo("1234567890"); student[2].setmRollNo(3); } //student airray conviewts to json airray public String writeDataTOJOSON() { //JONS airray created here mStudArray=new JSONArray(); initData(); for(int i=0;i<3;i++) { mStudArray.put(createJOSNObject(student[i])); } return mStudArray.toString(); } //student object conviewted into json object public JSONObject createJOSNObject(Student student) { try { mStudObject=new JSONObject(); mStudObject.put(Constants.NAME,student.getmStudName()); mStudObject.put(Constants.PHONE_NUMBER,student.getmPhoneNo()); mStudObject.put(Constants.ROLL_NUMBER,student.getmRollNo()); } catch (JSONException e) { e.printStackTrace(); } return mStudObject; } } } public class CreateJSON { private JSONArray mStudArray; private JSONObject mStudObject; Student [] student=new Student[3] ; public CreateJSON() { } public void initData() { //date wirte in student airray student[0]=new Student(); student[0].setmStudName("Vikas"); student[0].setmPhoneNo("1234567890"); student[0].setmRollNo(1); student[1]=new Student(); student[1].setmStudName("Rahul"); student[1].setmPhoneNo("1234567890"); student[1].setmRollNo(2); student[2]=new Student(); student[2].setmStudName("Raj"); student[2].setmPhoneNo("1234567890"); student[2].setmRollNo(3); } //student airray conviewts to json airray public String writeDataTOJOSON() { //JONS airray created here mStudArray=new JSONArray(); initData(); for(int i=0;i<3;i++) { mStudArray.put(createJOSNObject(student[i])); } return mStudArray.toString(); } //student object conviewted into json object public JSONObject createJOSNObject(Student student) { try { mStudObject=new JSONObject(); mStudObject.put(Constants.NAME,student.getmStudName()); mStudObject.put(Constants.PHONE_NUMBER,student.getmPhoneNo()); mStudObject.put(Constants.ROLL_NUMBER,student.getmRollNo()); } catch (JSONException e) { e.printStackTrace(); } return mStudObject; } } } public class CreateJSON { private JSONArray mStudArray; private JSONObject mStudObject; Student [] student=new Student[3] ; public CreateJSON() { } public void initData() { //date wirte in student airray student[0]=new Student(); student[0].setmStudName("Vikas"); student[0].setmPhoneNo("1234567890"); student[0].setmRollNo(1); student[1]=new Student(); student[1].setmStudName("Rahul"); student[1].setmPhoneNo("1234567890"); student[1].setmRollNo(2); student[2]=new Student(); student[2].setmStudName("Raj"); student[2].setmPhoneNo("1234567890"); student[2].setmRollNo(3); } //student airray conviewts to json airray public String writeDataTOJOSON() { //JONS airray created here mStudArray=new JSONArray(); initData(); for(int i=0;i<3;i++) { mStudArray.put(createJOSNObject(student[i])); } return mStudArray.toString(); } //student object conviewted into json object public JSONObject createJOSNObject(Student student) { try { mStudObject=new JSONObject(); mStudObject.put(Constants.NAME,student.getmStudName()); mStudObject.put(Constants.PHONE_NUMBER,student.getmPhoneNo()); mStudObject.put(Constants.ROLL_NUMBER,student.getmRollNo()); } catch (JSONException e) { e.printStackTrace(); } return mStudObject; } } } public class CreateJSON { private JSONArray mStudArray; private JSONObject mStudObject; Student [] student=new Student[3] ; public CreateJSON() { } public void initData() { //date wirte in student airray student[0]=new Student(); student[0].setmStudName("Vikas"); student[0].setmPhoneNo("1234567890"); student[0].setmRollNo(1); student[1]=new Student(); student[1].setmStudName("Rahul"); student[1].setmPhoneNo("1234567890"); student[1].setmRollNo(2); student[2]=new Student(); student[2].setmStudName("Raj"); student[2].setmPhoneNo("1234567890"); student[2].setmRollNo(3); } //student airray conviewts to json airray public String writeDataTOJOSON() { //JONS airray created here mStudArray=new JSONArray(); initData(); for(int i=0;i<3;i++) { mStudArray.put(createJOSNObject(student[i])); } return mStudArray.toString(); } //student object conviewted into json object public JSONObject createJOSNObject(Student student) { try { mStudObject=new JSONObject(); mStudObject.put(Constants.NAME,student.getmStudName()); mStudObject.put(Constants.PHONE_NUMBER,student.getmPhoneNo()); mStudObject.put(Constants.ROLL_NUMBER,student.getmRollNo()); } catch (JSONException e) { e.printStackTrace(); } return mStudObject; } } } public class CreateJSON { private JSONArray mStudArray; private JSONObject mStudObject; Student [] student=new Student[3] ; public CreateJSON() { } public void initData() { //date wirte in student airray student[0]=new Student(); student[0].setmStudName("Vikas"); student[0].setmPhoneNo("1234567890"); student[0].setmRollNo(1); student[1]=new Student(); student[1].setmStudName("Rahul"); student[1].setmPhoneNo("1234567890"); student[1].setmRollNo(2); student[2]=new Student(); student[2].setmStudName("Raj"); student[2].setmPhoneNo("1234567890"); student[2].setmRollNo(3); } //student airray conviewts to json airray public String writeDataTOJOSON() { //JONS airray created here mStudArray=new JSONArray(); initData(); for(int i=0;i<3;i++) { mStudArray.put(createJOSNObject(student[i])); } return mStudArray.toString(); } //student object conviewted into json object public JSONObject createJOSNObject(Student student) { try { mStudObject=new JSONObject(); mStudObject.put(Constants.NAME,student.getmStudName()); mStudObject.put(Constants.PHONE_NUMBER,student.getmPhoneNo()); mStudObject.put(Constants.ROLL_NUMBER,student.getmRollNo()); } catch (JSONException e) { e.printStackTrace(); } return mStudObject; } } 

    // Esta class imprimirá o json criado

     public class PrintJOSN { private CreateJSON createJSON; private JSONArray mStudnetArray; public String printJSONData() { createJSON=new CreateJSON(); //JSONArray created here. String jons=createJSON.writeDataTOJOSON(); //JONS in String fomairt Logger.debug("json"+mStudnetArray); //JOSN airray create from string try { mStudnetArray=new JSONArray(jons); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //pairse airray pairse here for(int i=0;i<3;i++) { try { JSONObject studentObj=(JSONObject) mStudnetArray.get(i); String name=studentObj.getString("name"); String phone=studentObj.getString("phone_number"); Integer rollnumber=studentObj.getInt("roll_number"); Logger.debug("Student Object::: "+name+" "+phone+" "+rollnumber); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } } } public class PrintJOSN { private CreateJSON createJSON; private JSONArray mStudnetArray; public String printJSONData() { createJSON=new CreateJSON(); //JSONArray created here. String jons=createJSON.writeDataTOJOSON(); //JONS in String fomairt Logger.debug("json"+mStudnetArray); //JOSN airray create from string try { mStudnetArray=new JSONArray(jons); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //pairse airray pairse here for(int i=0;i<3;i++) { try { JSONObject studentObj=(JSONObject) mStudnetArray.get(i); String name=studentObj.getString("name"); String phone=studentObj.getString("phone_number"); Integer rollnumber=studentObj.getInt("roll_number"); Logger.debug("Student Object::: "+name+" "+phone+" "+rollnumber); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } } } public class PrintJOSN { private CreateJSON createJSON; private JSONArray mStudnetArray; public String printJSONData() { createJSON=new CreateJSON(); //JSONArray created here. String jons=createJSON.writeDataTOJOSON(); //JONS in String fomairt Logger.debug("json"+mStudnetArray); //JOSN airray create from string try { mStudnetArray=new JSONArray(jons); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //pairse airray pairse here for(int i=0;i<3;i++) { try { JSONObject studentObj=(JSONObject) mStudnetArray.get(i); String name=studentObj.getString("name"); String phone=studentObj.getString("phone_number"); Integer rollnumber=studentObj.getInt("roll_number"); Logger.debug("Student Object::: "+name+" "+phone+" "+rollnumber); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } } } public class PrintJOSN { private CreateJSON createJSON; private JSONArray mStudnetArray; public String printJSONData() { createJSON=new CreateJSON(); //JSONArray created here. String jons=createJSON.writeDataTOJOSON(); //JONS in String fomairt Logger.debug("json"+mStudnetArray); //JOSN airray create from string try { mStudnetArray=new JSONArray(jons); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //pairse airray pairse here for(int i=0;i<3;i++) { try { JSONObject studentObj=(JSONObject) mStudnetArray.get(i); String name=studentObj.getString("name"); String phone=studentObj.getString("phone_number"); Integer rollnumber=studentObj.getInt("roll_number"); Logger.debug("Student Object::: "+name+" "+phone+" "+rollnumber); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } } return nulo; public class PrintJOSN { private CreateJSON createJSON; private JSONArray mStudnetArray; public String printJSONData() { createJSON=new CreateJSON(); //JSONArray created here. String jons=createJSON.writeDataTOJOSON(); //JONS in String fomairt Logger.debug("json"+mStudnetArray); //JOSN airray create from string try { mStudnetArray=new JSONArray(jons); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //pairse airray pairse here for(int i=0;i<3;i++) { try { JSONObject studentObj=(JSONObject) mStudnetArray.get(i); String name=studentObj.getString("name"); String phone=studentObj.getString("phone_number"); Integer rollnumber=studentObj.getInt("roll_number"); Logger.debug("Student Object::: "+name+" "+phone+" "+rollnumber); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } } } public class PrintJOSN { private CreateJSON createJSON; private JSONArray mStudnetArray; public String printJSONData() { createJSON=new CreateJSON(); //JSONArray created here. String jons=createJSON.writeDataTOJOSON(); //JONS in String fomairt Logger.debug("json"+mStudnetArray); //JOSN airray create from string try { mStudnetArray=new JSONArray(jons); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //pairse airray pairse here for(int i=0;i<3;i++) { try { JSONObject studentObj=(JSONObject) mStudnetArray.get(i); String name=studentObj.getString("name"); String phone=studentObj.getString("phone_number"); Integer rollnumber=studentObj.getInt("roll_number"); Logger.debug("Student Object::: "+name+" "+phone+" "+rollnumber); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } } 

    // Atividade principal

     public class JSONPairserActivity extends Activity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); PrintJOSN printJOSN=new PrintJOSN(); printJOSN.printJSONData(); } } } public class JSONPairserActivity extends Activity { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); PrintJOSN printJOSN=new PrintJOSN(); printJOSN.printJSONData(); } } 

    Modelo estudantil:

     public class Student { private String mStudName; private String mPhoneNo; private Integer mRollNo; public String getmStudName() { return mStudName; } public void setmStudName(String mStudName) { this.mStudName = mStudName; } public String getmPhoneNo() { return mPhoneNo; } public void setmPhoneNo(String mPhoneNo) { this.mPhoneNo = mPhoneNo; } public Integer getmRollNo() { return mRollNo; } public void setmRollNo(Integer mRollNo) { this.mRollNo = mRollNo; } } } public class Student { private String mStudName; private String mPhoneNo; private Integer mRollNo; public String getmStudName() { return mStudName; } public void setmStudName(String mStudName) { this.mStudName = mStudName; } public String getmPhoneNo() { return mPhoneNo; } public void setmPhoneNo(String mPhoneNo) { this.mPhoneNo = mPhoneNo; } public Integer getmRollNo() { return mRollNo; } public void setmRollNo(Integer mRollNo) { this.mRollNo = mRollNo; } } } public class Student { private String mStudName; private String mPhoneNo; private Integer mRollNo; public String getmStudName() { return mStudName; } public void setmStudName(String mStudName) { this.mStudName = mStudName; } public String getmPhoneNo() { return mPhoneNo; } public void setmPhoneNo(String mPhoneNo) { this.mPhoneNo = mPhoneNo; } public Integer getmRollNo() { return mRollNo; } public void setmRollNo(Integer mRollNo) { this.mRollNo = mRollNo; } } } public class Student { private String mStudName; private String mPhoneNo; private Integer mRollNo; public String getmStudName() { return mStudName; } public void setmStudName(String mStudName) { this.mStudName = mStudName; } public String getmPhoneNo() { return mPhoneNo; } public void setmPhoneNo(String mPhoneNo) { this.mPhoneNo = mPhoneNo; } public Integer getmRollNo() { return mRollNo; } public void setmRollNo(Integer mRollNo) { this.mRollNo = mRollNo; } } } public class Student { private String mStudName; private String mPhoneNo; private Integer mRollNo; public String getmStudName() { return mStudName; } public void setmStudName(String mStudName) { this.mStudName = mStudName; } public String getmPhoneNo() { return mPhoneNo; } public void setmPhoneNo(String mPhoneNo) { this.mPhoneNo = mPhoneNo; } public Integer getmRollNo() { return mRollNo; } public void setmRollNo(Integer mRollNo) { this.mRollNo = mRollNo; } } } public class Student { private String mStudName; private String mPhoneNo; private Integer mRollNo; public String getmStudName() { return mStudName; } public void setmStudName(String mStudName) { this.mStudName = mStudName; } public String getmPhoneNo() { return mPhoneNo; } public void setmPhoneNo(String mPhoneNo) { this.mPhoneNo = mPhoneNo; } public Integer getmRollNo() { return mRollNo; } public void setmRollNo(Integer mRollNo) { this.mRollNo = mRollNo; } } } public class Student { private String mStudName; private String mPhoneNo; private Integer mRollNo; public String getmStudName() { return mStudName; } public void setmStudName(String mStudName) { this.mStudName = mStudName; } public String getmPhoneNo() { return mPhoneNo; } public void setmPhoneNo(String mPhoneNo) { this.mPhoneNo = mPhoneNo; } public Integer getmRollNo() { return mRollNo; } public void setmRollNo(Integer mRollNo) { this.mRollNo = mRollNo; } } 

    Classe constante:

     public class Constants { public static final String NAME="name"; public static final String ROLL_NUMBER="roll_number"; public static final String PHONE_NUMBER="phone_number"; } 

    Classe Logger:

     public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } writer.flush (); public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } catch (Exception e) { public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } } public class Logger { public static final String APP_ID = "androidapp"; public static String logDir = "/androidapp"; public static String logFileName = "/log.txt"; public static boolean writeLogsToFile = false; public static final int LOG_LEVEL_VERBOSE = 4; public static final int LOG_LEVEL_DEBUG = 3; public static final int LOG_LEVEL_INFO = 2; public static final int LOG_LEVEL_ERROR = 1; public static final int LOG_LEVEL_OFF = 0; public static final int CURRENT_LOG_LEVEL = LOG_LEVEL_DEBUG; public static void log(String message, int logLevel) { if (logLevel > CURRENT_LOG_LEVEL) { return; } else { Log.v(APP_ID, message); if (writeLogsToFile) { writeToFile(message); } } } private static void writeToFile(String message) { try { File sdCaird = Environment.getExternalStorageDirectory(); File dir = new File(sdCaird.getAbsolutePath() + logDir); dir.mkdirs(); File file = new File(dir, logFileName); PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file, true), 8 * 1024)); writer.println(APP_ID + " " + new Date().toString() + " : " + message); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void viewbose(String message) { log(message, LOG_LEVEL_VERBOSE); } public static void debug(String message) { log(message, LOG_LEVEL_DEBUG); } public static void error(String message) { log(message, LOG_LEVEL_ERROR); } public static void info(String message) { log(message, LOG_LEVEL_INFO); } } 

    Verifique seu registro, ele mostrairá o json criado e o json impresso.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.