本博客发表于我大二的时候,写了一个比较简单的天气预报,不是很规范,但也算是一次进步,大神勿喷!
天气API:http://apicloud.mob.com/v1/weather/query?key=appkey&city=city
软件第一次启动会让你设置城市
我这里只添加了部分城市,也可以搜索,但搜索没有的城市会报错,这个问题之前解决过,但好像没有效果,PS后来就没有管了
下面是实现代码
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: String json = (String) msg.obj; // Log.i("日志3", ""+json); try { jiexi(json1); } catch (JSONException e) { e.printStackTrace(); } break; case 2: initListView(); break; case 3: break; } } }; private String s=""; public void test2(View view){ //获取城市 String city = ed_getcity.getText().toString(); if (city.equals("")){ Toast.makeText(this,"请输入城市",Toast.LENGTH_SHORT).show(); Log.i(TAG, "NULL"); }else { String url="http://apicloud.mob.com/v1/weather/query?key=appkey&city="; final String url1=url+city; new Thread (){ @Override public void run() { try { s= yanz (url1);//请求数据 } catch (Exception e) { e.printStackTrace (); } } }.start (); if(s.equals ("success")){ SharedPreferences sp=getSharedPreferences("city",0); SharedPreferences.Editor edit = sp.edit(); edit.putString("city",city); edit.commit(); startActivity(new Intent(this,Home_Activity.class)); //citys1=null; finish(); }else { Toast.makeText (this,s+"",Toast.LENGTH_SHORT).show (); } } } public String yanz(String url1) throws Exception { // OkHttpClient client=new OkHttpClient (); Request request=new Request.Builder () .url (url1) .build (); Response response=client.newCall (request).execute (); String json=response.body ().string (); JSONObject object=new JSONObject (json); final String msg = object.optString ("msg"); return msg; } private void initListView() { ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.layout2, R.id.tv_ct1, citys1); list_item2.setAdapter(adapter); list_item2.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { SharedPreferences sp = getSharedPreferences("city", 0); SharedPreferences.Editor edit = sp.edit(); if(citys1.equals ("西安")){ }else { edit.putString("city", citys1[position]); edit.commit(); Toast.makeText(shezhi_Activity.this, "设置成功!", Toast.LENGTH_SHORT).show(); startActivity(new Intent(shezhi_Activity.this, Home_Activity.class)); finish(); } } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shezhi_); try { initView(); } catch (Exception e) { e.printStackTrace(); } } private void initView() throws Exception { ed_getcity=findViewById(R.id.ed_getcity); list_item2 = findViewById(R.id.list_item2); citys1 = new String[70]; new Thread() { @Override public void run() { get1(url); } }.start(); } public void get1(String URL) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(URL) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { Message msg = new Message(); msg.what = 1; msg.obj = response.body().string(); handler.sendMessage(msg); } }); } public void jiexi(String json) throws JSONException { JSONObject object = new JSONObject(json); JSONArray provinces = object.optJSONArray("provinces"); for (int a = 0; a < 33; a++) { JSONObject object1 = provinces.optJSONObject(a); JSONArray citys = object1.optJSONArray("citys"); for (int b = 0; b < 3; b++) { Log.i("日志4", " " + ints); if (ints < 70) { JSONObject object2 = citys.optJSONObject(b); String citysName = object2.optString("citysName"); citys1[ints] = citysName.substring(0, citysName.length() - 1); } ints++; } } Message msg = new Message(); msg.what = 2; handler.sendMessage(msg); } }
代码有点乱,也不是很规范,希望不要介意
设置好后就会跳到天气显示界面
具体是那个城市我用SP存储,到这个Activity我通过SP取出来
private void initView() { SharedPreferences sp = getSharedPreferences("city", 0); String city = sp.getString("city", ""); url = url + city;
这个界面就是ListView 比较麻烦,需要判断天气情况来显示不同的图片
我是通过事先吧每个图片的ID拿到,放到一个数组里,然后通过请求下来的数据解析后一一比对,在排序,通过自定义的适配器来显示具体代码如下:
private void initDate2() {//判断ListView 中天气所对应的图片 String s = if_date[0]; String s1 = if_date[1]; String s2 = if_date[2]; String s3 = if_date[3]; String s4 = if_date[4]; if (s.equals("多云")) { ints[0] = R.mipmap.ic_1; } else if (s.equals("中雨")) { ints[0] = R.mipmap.ic_4; } else if (s.equals("大雨")) { ints[0] = R.mipmap.ic_5; } else if (s.equals("阵雨")) { ints[0] = R.mipmap.ic_5; } else if (s.equals("晴")) { ints[0] = R.mipmap.ic_8; } else if (s.equals("阴")) { ints[0] = R.mipmap.ic_9; } else if (s.equals("小雨")) { ints[0] = R.mipmap.ic_15; } else { ints[0] = R.mipmap.ic_8; } if (s1.equals("多云")) { ints[1] = R.mipmap.ic_1; } else if (s1.equals("中雨")) { ints[1] = R.mipmap.ic_4; } else if (s1.equals("大雨")) { ints[1] = R.mipmap.ic_5; } else if (s1.equals("阵雨")) { ints[1] = R.mipmap.ic_5; } else if (s1.equals("晴")) { ints[1] = R.mipmap.ic_8; } else if (s1.equals("阴")) { ints[1] = R.mipmap.ic_9; } else if (s1.equals("小雨")) { ints[1] = R.mipmap.ic_15; } else { ints[1] = R.mipmap.ic_8; } if (s2.equals("多云")) { ints[2] = R.mipmap.ic_1; } else if (s2.equals("中雨")) { ints[2] = R.mipmap.ic_4; } else if (s2.equals("大雨")) { ints[2] = R.mipmap.ic_5; } else if (s2.equals("阵雨")) { ints[2] = R.mipmap.ic_5; } else if (s2.equals("晴")) { ints[2] = R.mipmap.ic_8; } else if (s2.equals("阴")) { ints[2] = R.mipmap.ic_9; } else if (s2.equals("小雨")) { ints[2] = R.mipmap.ic_15; } else { ints[2] = R.mipmap.ic_8; } if (s3.equals("多云")) { ints[3] = R.mipmap.ic_1; } else if (s3.equals("中雨")) { ints[3] = R.mipmap.ic_4; } else if (s3.equals("大雨")) { ints[3] = R.mipmap.ic_5; } else if (s3.equals("阵雨")) { ints[3] = R.mipmap.ic_5; } else if (s3.equals("晴")) { ints[3] = R.mipmap.ic_8; } else if (s3.equals("阴")) { ints[3] = R.mipmap.ic_9; } else if (s3.equals("小雨")) { ints[3] = R.mipmap.ic_15; } else { ints[3] = R.mipmap.ic_8; } if (s4.equals("多云")) { ints[4] = R.mipmap.ic_1; } else if (s4.equals("中雨")) { ints[4] = R.mipmap.ic_4; } else if (s4.equals("大雨")) { ints[4] = R.mipmap.ic_5; } else if (s4.equals("阵雨")) { ints[4] = R.mipmap.ic_5; } else if (s4.equals("晴")) { ints[4] = R.mipmap.ic_8; } else if (s4.equals("阴")) { ints[4] = R.mipmap.ic_9; } else if (s4.equals("小雨")) { ints[4] = R.mipmap.ic_15; } else { ints[4] = R.mipmap.ic_8; } for (int a = 0; a < 5; a++) { final Map<String, String> map = date1.get(a); final String date = map.get("date"); final String dayTime = map.get("dayTime"); final String temperature = map.get("temperature"); d1[a] = date; d2[a] = dayTime; d3[a] = temperature; } }
其他的都比较简单;网络请求我用的OKHTTP框架,解析JSON就不用我说了吧
整个程序的逻辑比较简单,源码我会下次分享出来,希望大神勿喷,第一次写博客,多多关照!