Olá…boa noite!
Queria saber se alguém poderia me ajudar com o problema que estou tendo conforme abaixo:
Estou tentando popular minha “response” porém ela sempre está saindo null.
Segue meu código.
package br.com.tapanovisual.tvisual;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**Created by thiago.c.melo on 15/03/2016.*/
public class ServiceHandler {
public String response; public final static List<String> lista = null; public final static int GET = 1; public final static int POST = 2;
public ServiceHandler(){
}
public String ServiceCall(String url, int method){ return this.ServiceCall(url, method, null); }
private String ServiceCall(String url, int method, Object o) {
try { URL url2 = new URL("http://tapanovisual.esy.es/selectUltimasReservas.php"); HttpURLConnection conn = (HttpURLConnection) url2.openConnection(); conn.connect();
//Testando metodo enviado if (method == POST){
}else if(method == GET){
InputStream stream = conn.getInputStream(); BufferedReader buffer = new BufferedReader(new InputStreamReader(stream)); StringBuffer sb = new StringBuffer();
String line = ""; while ((line = buffer.readLine()) != null){ sb.append(line); }
String finalJson = sb.toString(); JSONObject parentObject = new JSONObject(finalJson);
response = parentObject.getString("nome");
}
} catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); }
return response; }
}
Abaixo segue meu Json.
{“estabelecimentos”:[{“id”:“1”,“nome”:“Raphas Fashion Hair”},{“id”:“2”,“nome”:“Estacao Beleza”},{“id”:“3”,“nome”:“Beleza Natural”}],“sucesso”:1}
Obrigado!