Android——写一款属于自己的天气预报

直接上图:

这里写图片描述

在这里,先来提供天气接口:

http://wthrcdn.etouch.cn/weather_mini?city=

在city后面需要查询的城市即可,这是JSON数据,需要解析自己需要的信息。

这里写图片描述

上次已经说到了如何解析,现在直接附代码:

package com.example.weathertest2;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Request.Builder;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private EditText main_et;
    private TextView wendu,tishi,fengxiang,fengli,high,type,low,date;
    private ProgressDialog pd;
    private String tianqi[];
    private MediaPlayer player;
    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            if(msg.what==10)
            {
                pd.dismiss();

                String content=(String) msg.obj;
                try {
                    tianqi=getJieXi(content);
                    wendu.setText("当前温度: \t"+tianqi[0]);
                    tishi.setText("温馨提示: \t"+tianqi[1]);
                    fengxiang.setText("风向: \t"+tianqi[2]);
                    fengli.setText("风力: \t"+tianqi[3]);
                    high.setText("最"+tianqi[4]);
                    type.setText("天气类型: \t"+tianqi[5]);
                    low.setText("最"+tianqi[6]);
                    date.setText("日期: \t"+tianqi[7]);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initViews();

        player=MediaPlayer.create(this, R.raw.wangtian);

        player.start();

    }
    protected String[] getJieXi(String content) throws Exception {
        // TODO Auto-generated method stub
        JSONObject obj=new JSONObject(content);
        JSONObject data = obj.getJSONObject("data");
        String wendu = data.getString("wendu");
        String tishi=data.getString("ganmao");
        JSONArray forecast = data.getJSONArray("forecast");
        JSONObject obj2=(JSONObject) forecast.get(0);
        String fengxiang=obj2.getString("fengxiang");
        String fengli=obj2.getString("fengli");
        String high=obj2.getString("high");
        String type=obj2.getString("type");
        String low=obj2.getString("low");
        String date=obj2.getString("date");
        String tianqi[]={wendu,tishi,fengxiang,fengli,high,type,low,date};
        return tianqi;
    }
    private String getCity() {
        // TODO Auto-generated method stub
        String city=main_et.getText().toString().trim();
        return city;
    }
    private void initViews() {
        // TODO Auto-generated method stub
        main_et=(EditText) findViewById(R.id.main_et);
        wendu =(TextView) findViewById(R.id.wendu);
        tishi =(TextView) findViewById(R.id.tishi);
        fengxiang =(TextView) findViewById(R.id.fengxiang);
        fengli =(TextView) findViewById(R.id.fengli);
        high =(TextView) findViewById(R.id.high);
        type =(TextView) findViewById(R.id.type);
        low=(TextView) findViewById(R.id.low);
        date =(TextView) findViewById(R.id.date);

    }

    public void myclick(View v) throws Exception
    {
        pd=new ProgressDialog(this);
        pd.setMessage("正在查询,请稍候...");
        pd.show();
        OkHttpClient client=new OkHttpClient();
        Request.Builder builder=new Builder();
        builder.get();
        builder.url("http://wthrcdn.etouch.cn/weather_mini?city="+URLEncoder.encode(getCity(), "UTF-8"));
        Request request = builder.build();
        Call call=client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onResponse(Response arg0) throws IOException {
                // TODO Auto-generated method stub
                String content=arg0.body().string();
                Message msg=Message.obtain();
                msg.obj=content;
                msg.what=10;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(Request arg0, IOException arg1) {
                // TODO Auto-generated method stub

            }
        });
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(player!=null)
        {
            player.reset();
            player.release();
        }
    }


}

在这里加入了多媒体的知识,添加了背景音乐,之后会慢慢提到,这个不影响当前代

码。

URLEncoder.encode(getCity(), “UTF-8”))这里用到的这个方法是将输入的中文转换

为UTF-8的编码方式,不然会出现乱码。

写到这里,可以去装装逼啊!~~~

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值