我知道这个问题已经被问过很多次了,但是到目前为止,以前给出的答案都没有对我有用,这是我的代码:
<html>
<head>
<title>Programa</title>
</head>
<body>
<div>
Introduzca una palabra a buscar:
<input type="text" id="INPUT">
<%@ page import="java.net.URL" %>
<%@ page import="com.google.gson.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.Reader" %>
<%@ page import="javax.swing.*" %>
<%@ page import="java.awt.event.*;" %>
<%!
int min(int a,int b) {
return (a>b)? b:a;
}
int edit_distance(String a,String b) {
int n = a.length(), m = b.length(),costo;
int[][] mat = new int[n+1][m+1];
for(int i=0;i<=n;++i) mat[i][0] = i;
for(int j=0;j<=m;++j) mat[0][j] = j;
for(int i=1;i<=n;++i) {
for(int j=1;j<=m;++j) {
costo = a.charAt(i-1) == b.charAt(j-1)? 1 : 0;
mat[i][j] = min(min(mat[i-1][j] + 1,mat[i-1][j-1] + costo),mat[i][j-1] + 1);
}
}
return mat[n][m];
}
String resultados_de_la_busqueda(String search) { //Básicamente lo que hace esta función es devolverte una cadena con los resultados de la búsqueda
StringBuffer RES = new StringBuffer("<html>");
String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; //El enlace para buscar en google
String charset = "UTF-8";
URL url;
Reader reader;
try {
url = new URL(google + URLEncoder.encode(search, charset));
try {
reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
for(int i=0;i<3;++i) {
RES.append(results.getResponseData().getResults().get(i).getTitle());
RES.append("<br/>");
RES.append("<a href=\"");
RES.append(results.getResponseData().getResults().get(i).getUrl());
RES.append("\">" + results.getResponseData().getResults().get(i).getUrl() + "</a>");
RES.append("<br/><br/>");
}
} catch(Exception e) {}
} catch(Exception e) {}
return RES.toString();
}
%>
<%
//Se supone que acá debo mostrar el resultado de la búsqueda...
out.println(resultados_de_la_busqueda("taringa"));
%>
</div>
</body>
</html>
这是我得到的错误:
org.apache.jasper.JasperException: No se puede compilar la clase para JSP:
Ha tenido lugar un error en la línea: 47 en el archivo jsp: /Programa.jsp
GoogleResults cannot be resolved to a type
Gson cannot be resolved to a type
对导致问题的原因有任何想法吗?
编辑1:
这是 GoogleResults 类:
import java.util.List;
public class GoogleResults {
private ResponseData responseData;
public ResponseData getResponseData() { return responseData; }
public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
public String toString() { return "ResponseData[" + responseData + "]"; }
static class ResponseData {
private List<Result> results;
public List<Result> getResults() { return results; }
public void setResults(List<Result> results) { this.results = results; }
public String toString() { return "Results[" + results + "]"; }
}
static class Result {
private String url;
private String title;
public String getUrl() { return url; }
public String getTitle() { return title; }
public void setUrl(String url) { this.url = url; }
public void setTitle(String title) { this.title = title; }
public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
}
}