返回信息流public class WeatherUtils {
public static String request(String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
String httpUrl = "http://apis.baidu.com/apistore/weatherservice/cityname" + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "0b822d024bcfcdbb1c4f5c141c3a4893");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
System.out.println(request("cityname=北京"));
}
}
百度API Store上的天气API,用java可以跑通,但是移植到Android上,就无法查询数据,求指教
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
String httpUrl = "http://apis.baidu.com/apistore/weatherservice/cityname";
String httpArg = "cityname=北京";
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("apikey","0b822d024bcfcdbb1c4f5c141c3a4893");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("tag",result+"-------");
super.run();
}
}.start();
}
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #26052同步于 2015/10/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖
新手求教,Android调用百度API Store的问题
fengshi698
2015/10/8镜像同步12 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
不是的,java那个可以跑通,httpurl最后的那个cityname是链接里面的,后面那个cityname=北京是参数,没有重复,我把Android里面run()方法中的代码在java中运行,也是正确的。
【 在 fuxuemingzhu 的大作中提到: 】
: 网页请求 cityname 单词写了两遍
你真的确定写两遍没问题?
【 在 fengshi698 (fengshi) 的大作中提到: 】
: 不是的,java那个可以跑通,httpurl最后的那个cityname是链接里面的,后面那个cityname=北京是参数,没有重复,我把Android里面run()方法中的代码在java中运行,也是正确的。
把Android的run方法中的代码放在Java中跑通了。
【 在 fuxuemingzhu (负雪明烛) 的大作中提到: 】
: 你真的确定写两遍没问题?
通过『我邮2.0』发布
返回信息不是提示了说:请输入正确的城市id\/城市名称\/城市拼音??
【 在 fengshi698 的大作中提到: 】
: public class WeatherUtils {
: public static String request(String httpArg) {
: BufferedReader reader = null;
: ...................
可是java的代码跟Android的一模一样啊,java的返回正确了。。。。
【 在 zf1992 的大作中提到: 】
: 返回信息不是提示了说:请输入正确的城市id\/城市名称\/城市拼音??
直接用HttpGet试试?
【 在 fengshi698 的大作中提到: 】
: 可是java的代码跟Android的一模一样啊,java的返回正确了。。。。