android开发练习

 引用 http://blog.csdn.net/jediael_lu/article/details/25811579

仿照这篇文章写一个类似的程序程序


碰上的问题一api更换;


查询之后获取最新api

api.openweathermap.org/data/2.5/weather?q=London


问题2

java.io.FileNotFoundException: http://api.openweathermap.org/data/2.5/weather?q=London

 

url1 = "api.openweathermap.org/data/2.5/weather?q="+location.getText().toString()+",uk&appid=44db6a862fba0b067b1930da0d769e98";
原来是需要apikey
 改完之后依然是问题2,进行debug 

发现问题出在这里URL url = new URL(url1 );
修改为
URL url = new URL("https://"+url1);
之后成功运行,但输出结果全是0.
发现问题
org.json.JSONException: End of input at character 0 of 
改写部分语句
//等待上述线程完成执行后再返回jsonText。

try {
    Thread.sleep(1000);
    return jsonText;
} catch (InterruptedException e) {
    e.printStackTrace();
}
return null;
java.net.SocketTimeoutException: failed to connect to api.openweathermap.org/192.241.169.168 (port 443) after 15000ms
加了很多断点进入debug查看,发现主线程已经结束了,网络线程还没获取到数据,此时jsontext当然为空
修改源码,将获取网络数据放入AsyncTask中,
class  MyAsycTask extends AsyncTask<String,Void,String>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.print("异步线程开始");
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        WeatherBean weatherBean = JSONUtil.getWeatherBean(s);
        country.setText(weatherBean.getCountry());
        humidity.setText(weatherBean.getHumidity() + "");
        pressure.setText(weatherBean.getPressure() + "");
        temperature.setText(weatherBean.getTemperature() + "");
        System.out.println("test2");
    }

    @Override
    protected String doInBackground(String... params) {
        InputStream is =null;
        BufferedReader in = null;
        String jsonText="";
        URL url= null;
        try {
            url = new URL(params[0]);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();

            is = conn.getInputStream();
            in = new BufferedReader(new InputStreamReader(is));
            String line = "";
            while((line = in.readLine()) != null){
                jsonText += line;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                in.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return jsonText;
    }
}
调试了两次之后,成功获取到数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值