Cita:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.RequestAcceptEncod ing;
import org.apache.http.client.protocol.ResponseContentEnc oding;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
public class ListCitiesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
getCityList(req, resp);
resp.setContentType("text/html");
}
private void getCityList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.addRequestInterceptor(new RequestAcceptEncoding());
httpclient.addResponseInterceptor(new ResponseContentEncoding());
String page = req.getParameter("pagina");
String url;
if(page == null) {
url = "http://api.despegar.com/cities";
} else {
url = "http://api.despegar.com/cities?page=" + page;
}
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("X-ApiKey", "[mi key]");
try {
HttpResponse response1 = httpclient.execute(httpGet);
System.out.println(response1.getStatusLine());
if (response1.getStatusLine().getStatusCode() == 200) {
HttpEntity entity1 = response1.getEntity();
// Primera implementacion, sacar el JSON como HTML
// entity1.writeTo(resp.getOutputStream());
String contents = readContents(entity1);
} else {
resp.getOutputStream().print(
"Hubo un error llamando al servicio "
+ response1.getStatusLine());
}
} finally {
httpGet.releaseConnection();
}
}
private String readContents(HttpEntity entity1) throws IllegalStateException, IOException {
StringBuffer sb = new StringBuffer();
String inputLine;
BufferedReader in = new BufferedReader(new InputStreamReader(
entity1.getContent()));
try {
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.RequestAcceptEncod ing;
import org.apache.http.client.protocol.ResponseContentEnc oding;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
public class ListCitiesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
getCityList(req, resp);
resp.setContentType("text/html");
}
private void getCityList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.addRequestInterceptor(new RequestAcceptEncoding());
httpclient.addResponseInterceptor(new ResponseContentEncoding());
String page = req.getParameter("pagina");
String url;
if(page == null) {
url = "http://api.despegar.com/cities";
} else {
url = "http://api.despegar.com/cities?page=" + page;
}
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("X-ApiKey", "[mi key]");
try {
HttpResponse response1 = httpclient.execute(httpGet);
System.out.println(response1.getStatusLine());
if (response1.getStatusLine().getStatusCode() == 200) {
HttpEntity entity1 = response1.getEntity();
// Primera implementacion, sacar el JSON como HTML
// entity1.writeTo(resp.getOutputStream());
String contents = readContents(entity1);
} else {
resp.getOutputStream().print(
"Hubo un error llamando al servicio "
+ response1.getStatusLine());
}
} finally {
httpGet.releaseConnection();
}
}
private String readContents(HttpEntity entity1) throws IllegalStateException, IOException {
StringBuffer sb = new StringBuffer();
String inputLine;
BufferedReader in = new BufferedReader(new InputStreamReader(
entity1.getContent()));
try {
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}