Srs,
O metodo post funciona quando uso o https://www.caelum.com.br/mobile, mas estou querendo testar num servidor localhost. Como faço isso? Segue abaixo a classe deste Curso Android e a classe do Curso WS rest ambos aqui do ALURA.
CLASSE1
public class ServidorProjetos {
public static void main(String[] args) throws IOException {
URI uri = URI.create("http://0.0.0.0:8080/");//peguei exemplo da internet
//URI uri = URI.create("http://localhost:8080/");
ResourceConfig config = new ResourceConfig().packages("br.com.alura.loja");
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, config);
System.out.println("Servidor rodando");
System.in.read();
server.stop();
}
}
CLASSE2
@Path("entregas")
public class EntregaResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public String busca(Long id){
return new EntregaDAO().busca(1l).toJSON();
}
}
CLASSE3
/**
* Created by antonio on 27/10/16.
*/
public class WebClient {
public String post(String json) {
try {
URL url = new URL("http://192.168.1.106:8080/entregas?id=1");
//URL url = new URL("https://www.caelum.com.br/mobile");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//connection.setRequestProperty("Content-type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(false);
/*PrintStream output = new PrintStream(connection.getOutputStream());
output.println(json);
Log.i("json", json);
*/
connection.connect();
int responseCode = connection.getResponseCode();
Log.i("WEBSERVICE", "Código de resposta : " + responseCode);
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder sb = new StringBuilder();
while (scanner.hasNext()){
sb.append(scanner.nextLine());
}
String resposta = sb.toString();
Log.d("json", resposta);
return resposta;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
CLASSE4
/**
* Created by antonio on 27/10/16.
*/
public class EnviaEntregasTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
EntregaDAO dao = new EntregaDAO(context);
List<Entrega> entregas = dao.buscaEntregas();
dao.close();
EntregaConverter conversor = new EntregaConverter();
String json = conversor.converteParaJSON(entregas);
WebClient client = new WebClient();
String resposta = client.post(json);
return resposta;
}
}
LOG DE ERRO
I/json: {"list":[{"entrega":[{"nome":"Nome1","nota":9},{"nome":"Nome2","nota":7},{"nome":"Nome3","nota":9}]}]}
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f0a3fe31cc0, error=EGL_SUCCESS
W/System.err: java.io.FileNotFoundException: http://192.168.1.106:8080/entregas
W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
W/System.err: at br.com.teste.agenda.WebClient$override.post(WebClient.java:33)
W/System.err: at br.com.teste.agenda.WebClient$override.access$dispatch(WebClient.java)
W/System.err: at br.com.teste.agenda.WebClient.post(WebClient.java:0)
W/System.err: at br.com.teste.agenda.EnviaEntregasTask.doInBackground(EnviaEntregasTask.java:40)
W/System.err: at br.com.teste.agenda.EnviaEntregasTask.doInBackground(EnviaEntregasTask.java:17)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f0a3fe31bc0, error=EGL_SUCCESS