http://api.map.baidu.com/place/v2/search?&query=%E9%93%B6%E8%A1%8C®ion=%E6%B5%8E%E5%8D%97&output=json&ak=E4805d16520de693a3fe707cdc962045
我想使用百度的Place API 得到检索到信息。求java方法!最后由实例!
2024-05-04 05:57:18
2024-05-04 05:15:18
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpRequestUtil {
/**
* 从url请求中获得返回的字符串
*
* @param requestUrl
* @return JSON字符串
*/
public static String HttpRequest(String requestUrl) {
StringBuffer sb = new StringBuffer();
InputStream ips = getInputStream(requestUrl);
InputStreamReader isreader = null;
try {
isreader = new InputStreamReader(ips, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(isreader);
String temp = null;
try {
while ((temp = bufferedReader.readLine()) != null) {
sb.append(temp);
}
bufferedReader.close();
isreader.close();
ips.close();
ips = null;
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 从请求的URL中获取返回的流数据
* @param requestUrl
* @return InputStream
*/
private static InputStream getInputStream(String requestUrl) {
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
try {
url = new URL(requestUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
in = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
}
数据是请求到了,可是为什么我一直返回staus:102啊?安全码什么都是对的····
我看了,首先你的URL中传的汉字要用UTF-8编码,然后这个令牌你用的是百度API上面的,这个不行。程序中用你自己的令牌,就可以了。
2024-05-04 00:19:32
2024-05-04 00:09:27