Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void jsonToObj(JSONArray resultadoDaPesquisa) {
- try {
- JSONObject jsonObject = null;
- JSONArray jsonArray = resultadoDaPesquisa;
- jsonObject = new JSONObject(resultadoDaPesquisa.toString());
- System.out.println("resultado " + jsonObject);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public class JsonReader {
- static String secretKeyCaptcha = "6Ld5NXMUAAAAAOXPv-EbwycOMHfoxJrSSdLcLl-I";
- private static String readAll(Reader rd) throws IOException {
- StringBuilder sb = new StringBuilder();
- int cp;
- while ((cp = rd.read()) != -1) {
- sb.append((char) cp);
- }
- return sb.toString();
- }
- public static JSONObject readJsonFromUrl(String cep) throws IOException, JSONException {
- InputStream is = new URL("https://viacep.com.br/ws/" + cep + "/json/").openStream();
- try {
- BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
- String jsonText = readAll(rd);
- JSONObject json = new JSONObject(jsonText);
- return json;
- } finally {
- is.close();
- }
- }
- public static JSONObject validarTokenRecaptcha(String token) throws IOException {
- System.out.println("https://www.google.com/recaptcha/api/siteverify?secret=" + secretKeyCaptcha + "&response=" + token);
- InputStream is = new URL(
- "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKeyCaptcha + "&response=" + token)
- .openStream();
- try {
- BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
- String jsonText = readAll(rd);
- JSONObject json = new JSONObject(jsonText);
- return json;
- } finally {
- is.close();
- }
- }
- }
- JSONObject jsonObj = new JSONObject();
- JsonReader jr = new JsonReader();
- jsonObj = jr.readJsonFromUrl(frete.getCepDestino());
- enderecoDestino = jsonObj.get("logradouro").toString() + " - " + jsonObj.get("bairro").toString()
- JSONObject jsonObject = null;
- try {
- for(int i = 0; i<resultadoDaPesquisa.length(); i++) {
- jsonObject = resultadoDaPesquisa.getJSONObject(i);
- String id = jsonObject.getString("id");
- String nome = jsonObject.getString("name");
- Anuncio anuncio = new Anuncio();
- anuncio.setUid(id);
- anuncio.setTitulo(nome);
- System.out.println(anuncio);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
Add Comment
Please, Sign In to add comment