字符串查询无法识别,因此plZ告诉我删除错误的确切答案 因为 如果它 是 错误 我不能 得到 该 精确 产量 在代码中,字符串查询无法识别,因此plZ告诉我删除错误的确切答案 因为 如果它 是 错误 我不能 得到 该 精确 输出
package sample.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.json.JSONArray; // JSON library from http://www.json.org/java/
import org.json.JSONObject;
/**
* Servlet implementation class GoogleMap
*/
@WebServlet("/GoogleMap")
public class GoogleMap extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String HTTP_REFERER = "http://www.shikshasolutions.net/";
public void GoogleQuery() {
makeQuery("viral");
makeQuery("info:http://google.com/");
makeQuery("site:google.com");
}
private void makeQuery(String query) {
System.out.println("\nQuerying for " + query);
}
/**
* @see HttpServlet#HttpServlet()
*/
public GoogleMap() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
// Convert spaces to +, etc. to make a valid URL
String query = URLEncoder.encode(query, "UTF-8");
URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q=" + query);
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", HTTP_REFERER);
// Get the JSON response
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
String response1 = builder.toString();
JSONObject json = new JSONObject(response1);
System.out.println("Total results = " + json.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount"));
JSONArray ja = json.getJSONObject("responseData")
.getJSONArray("results");
System.out.println("\nResults:");
for (int i = 0; i < ja.length(); i++) {
System.out.print(("ss"+i+1) + ". ");
JSONObject j = ja.getJSONObject(i);
System.out.println(j.getString("titleNoFormatting"));
System.out.println(j.getString("url"));
}
}
catch (Exception e) {
System.err.println("Something went wrong...");
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
答案 0 :(得分:1)
在你的行中:
String query = URLEncoder.encode(query, "UTF-8");
您正在尝试使用未实例化的查询值来实例化查询的值。是否应该用以下内容代替:
String query = URLEncoder.encode(request.getQueryString(), "UTF-8");
我不完全确定你在这里做什么或者你真正的问题是什么,但这是我要开始的地方。