Guest User

Untitled

a guest
Oct 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. public void jsonToObj(JSONArray resultadoDaPesquisa) {
  2.  
  3. try {
  4. JSONObject jsonObject = null;
  5. JSONArray jsonArray = resultadoDaPesquisa;
  6.  
  7. jsonObject = new JSONObject(resultadoDaPesquisa.toString());
  8. System.out.println("resultado " + jsonObject);
  9.  
  10.  
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14.  
  15.  
  16. }
  17.  
  18. public class JsonReader {
  19.  
  20. static String secretKeyCaptcha = "6Ld5NXMUAAAAAOXPv-EbwycOMHfoxJrSSdLcLl-I";
  21.  
  22. private static String readAll(Reader rd) throws IOException {
  23. StringBuilder sb = new StringBuilder();
  24. int cp;
  25. while ((cp = rd.read()) != -1) {
  26. sb.append((char) cp);
  27. }
  28. return sb.toString();
  29. }
  30.  
  31. public static JSONObject readJsonFromUrl(String cep) throws IOException, JSONException {
  32. InputStream is = new URL("https://viacep.com.br/ws/" + cep + "/json/").openStream();
  33. try {
  34. BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  35. String jsonText = readAll(rd);
  36. JSONObject json = new JSONObject(jsonText);
  37. return json;
  38. } finally {
  39. is.close();
  40. }
  41. }
  42.  
  43. public static JSONObject validarTokenRecaptcha(String token) throws IOException {
  44. System.out.println("https://www.google.com/recaptcha/api/siteverify?secret=" + secretKeyCaptcha + "&response=" + token);
  45. InputStream is = new URL(
  46. "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKeyCaptcha + "&response=" + token)
  47. .openStream();
  48. try {
  49. BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
  50. String jsonText = readAll(rd);
  51. JSONObject json = new JSONObject(jsonText);
  52. return json;
  53. } finally {
  54. is.close();
  55. }
  56. }
  57. }
  58.  
  59. JSONObject jsonObj = new JSONObject();
  60. JsonReader jr = new JsonReader();
  61. jsonObj = jr.readJsonFromUrl(frete.getCepDestino());
  62.  
  63. enderecoDestino = jsonObj.get("logradouro").toString() + " - " + jsonObj.get("bairro").toString()
  64.  
  65. JSONObject jsonObject = null;
  66. try {
  67. for(int i = 0; i<resultadoDaPesquisa.length(); i++) {
  68. jsonObject = resultadoDaPesquisa.getJSONObject(i);
  69. String id = jsonObject.getString("id");
  70. String nome = jsonObject.getString("name");
  71.  
  72. Anuncio anuncio = new Anuncio();
  73. anuncio.setUid(id);
  74. anuncio.setTitulo(nome);
  75.  
  76. System.out.println(anuncio);
  77. }
  78.  
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82.  
  83.  
  84. }
Add Comment
Please, Sign In to add comment