1
resposta

Usando WS localhost

Srs,

O metodo post funciona quando uso o https://wwww.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

package br.com.teste.agenda;

import java.io.IOException; import java.io.PrintStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Scanner;

/* 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/carrinhos"); //URL url = new URL("https://wwww.caelum.com.br/mobile");

HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-type", "application/json"); connection.setRequestProperty("Accept", "application/json");

connection.setDoOutput(true);

PrintStream output = new PrintStream(connection.getOutputStream()); output.println(json);

connection.connect();

Scanner scanner = new Scanner(connection.getInputStream()); String resposta = scanner.next(); System.out.println(resposta); return resposta; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }

}

CLASSE2

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(); } }

1 resposta

Olá Antonio,

Vou fechar essa dúvida porque ela está duplicada no seguinte link:

https://cursos.alura.com.br/forum/topico-usando-ws-localhost-26322