当前位置:   article > 正文

安卓简单的应用——天气预报_csdn安卓天气预报tomcat

csdn安卓天气预报tomcat

简单的事件逻辑:

从网上拿去天气数据,展示

然后做了一个数据清洗,然后还保存了一下最后的状态,以至于最后的时候可以打开所见即所得。

至于背景啥的,拿到源码以后自己改。

  1. package com.example.weather_forecast;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.SharedPreferences;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EdgeEffect;
  9. import android.widget.EditText;
  10. import android.widget.ListView;
  11. import android.widget.TextView;
  12. import java.io.BufferedReader;
  13. import java.io.InputStream;
  14. import java.io.InputStreamReader;
  15. import java.util.Arrays;
  16. import java.util.List;
  17. public class MainActivity extends AppCompatActivity {
  18. EditText editText;
  19. Button button;
  20. TextView tishi;
  21. ListView listView;
  22. SharedPreferences spfcode,spf;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. initDate();
  28. chaxun();
  29. moren();
  30. button.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View view) {
  33. String tempcity = editText.getText().toString();
  34. wealthinit(spfcode.getString(tempcity,"101010100"));
  35. tishi.setText("当前查询城市:"+ tempcity);
  36. if(!spfcode.getString(tempcity,"").equals("")){
  37. SharedPreferences.Editor editor = spf.edit();
  38. editor.putString("1",tempcity);
  39. editor.commit();
  40. }
  41. }
  42. });
  43. }
  44. private void moren() {
  45. if(spf.getString(1+"","").equals("")){
  46. SharedPreferences.Editor editor = spf.edit();
  47. editor.putString("1","北京");
  48. editor.commit();
  49. }
  50. wealthinit(spfcode.getString(spf.getString("1","北京"),"101010100"));
  51. }
  52. private void initDate() {
  53. editText = findViewById(R.id.per_gaichenshi);
  54. button = findViewById(R.id.per_citytijiao);
  55. tishi = findViewById(R.id.tishi);
  56. listView = findViewById(R.id.per_list);
  57. spfcode = getSharedPreferences("Citycode",MODE_PRIVATE);
  58. spf = getSharedPreferences("Personal",MODE_PRIVATE);
  59. }
  60. class MyTask extends AsyncTask<String,Void,String> {
  61. //获取json数据
  62. @Override
  63. protected String doInBackground(String... strings) {
  64. String jsonStr= null;
  65. try {
  66. jsonStr = Httputils.getJson(strings[0]);
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. System.out.println(jsonStr);
  71. return jsonStr;
  72. }
  73. //解析json数据,显示出来
  74. @Override
  75. protected void onPostExecute(String s) {
  76. try {
  77. Wealther[] wealthers = Httputils.parseJson(s);
  78. List<Wealther> list = Arrays.asList(wealthers);
  79. Tianqiadapt adapter = new Tianqiadapt(MainActivity.this, list);
  80. listView.setAdapter(adapter);
  81. int[] highTemp = new int[list.size()];
  82. int[] lowTemp = new int[list.size()];
  83. for(int i=0;i<list.size();i++) {
  84. Wealther temp = list.get(i);
  85. highTemp[i] = Integer.parseInt(temp.getTem_1());
  86. lowTemp[i] = Integer.parseInt(temp.getTem_2());
  87. }
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. private void wealthinit(String citycode) {
  94. try {
  95. new MyTask().execute("https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid="+citycode);
  96. // System.out.println(tempwealth);
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. private void chaxun(){
  102. InputStream is=getResources().openRawResource(R.raw.temp);
  103. InputStreamReader isr= null;//包装成IO流
  104. try {
  105. isr = new InputStreamReader(is,"UTF-8");
  106. BufferedReader bfr=new BufferedReader(isr); //包装成内存流
  107. String instring="";
  108. SharedPreferences.Editor editor = spfcode.edit();
  109. while((instring=bfr.readLine())!=null) {
  110. String[] split = instring.split("=");
  111. if(split.length==2){
  112. editor.putString(split[1],split[0]);
  113. editor.commit();
  114. }
  115. }
  116. }catch (Exception e){
  117. e.printStackTrace();
  118. }
  119. }
  120. }

软件源码

这一段是下载的,至于其他的嘛似乎不是很重要,随便整整也能出来。

  1. package com.example.weather_forecast;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import org.json.JSONArray;
  5. import org.json.JSONObject;
  6. import java.io.InputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import okhttp3.Call;
  10. import okhttp3.OkHttpClient;
  11. import okhttp3.Request;
  12. import okhttp3.Response;
  13. public class Httputils {
  14. public static Bitmap getImage(String url) throws Exception {
  15. Bitmap bitmap=null;
  16. URL a=new URL(url);
  17. HttpURLConnection connection= (HttpURLConnection) a.openConnection();
  18. int statusCode=connection.getResponseCode();
  19. if(statusCode==HttpURLConnection.HTTP_OK){
  20. InputStream inputStream=connection.getInputStream();
  21. bitmap= BitmapFactory.decodeStream(inputStream);
  22. inputStream.close();
  23. connection.disconnect();
  24. }
  25. return bitmap;
  26. }
  27. //获取json数据
  28. public static String getJson(String url) throws Exception {
  29. URL a=new URL(url);
  30. HttpURLConnection connection= (HttpURLConnection) a.openConnection();
  31. int statusCode=connection.getResponseCode();
  32. if(statusCode==HttpURLConnection.HTTP_OK){
  33. InputStream inputStream=connection.getInputStream();
  34. StringBuffer sb=new StringBuffer();
  35. byte[] buff=new byte[1024];
  36. int len;
  37. while((len=inputStream.read(buff))!=-1){
  38. sb.append(new String(buff,0,len));
  39. }
  40. inputStream.close();
  41. connection.disconnect();
  42. return sb.toString();
  43. }
  44. return null;
  45. }
  46. //解析json数据
  47. public static Wealther[] parseJson(String jsonStr) throws Exception {
  48. Wealther[] wealthers = new Wealther[7];
  49. for(int i=0;i<wealthers.length;i++){
  50. wealthers[i] = new Wealther();
  51. }
  52. JSONObject object=new JSONObject(jsonStr);
  53. wealthers[0].setUpdate(object.get("update_time").toString());
  54. wealthers[0].setCity(object.get("city").toString());
  55. JSONArray objects = object.getJSONArray("data");
  56. for(int i=0;i<objects.length();i++){
  57. JSONObject temp = objects.getJSONObject(i);
  58. if(i!=0){
  59. wealthers[i].setUpdate(wealthers[0].getUpdate());
  60. wealthers[i].setCity(wealthers[0].getCity());
  61. }
  62. wealthers[i].setDate(temp.get("day").toString());
  63. wealthers[i].setWealth(temp.getString("wea"));
  64. wealthers[i].setTem_1(temp.getString("tem"));
  65. wealthers[i].setTem_2(temp.getString("tem1"));
  66. wealthers[i].setTem_3(temp.getString("tem2"));
  67. wealthers[i].setShidu(temp.getString("humidity"));
  68. wealthers[i].setFenxiang(temp.getString("win"));
  69. wealthers[i].setSunup(temp.getString("sunrise"));
  70. wealthers[i].setSundown(temp.getString("sunset"));
  71. wealthers[i].setMiaoshu(temp.getString("narrative"));
  72. wealthers[i].setMoonup(temp.getString("moonrise"));
  73. wealthers[i].setMoondown(temp.getString("moonset"));
  74. wealthers[i].setZiwaixian(temp.getString("uvDescription"));
  75. }
  76. return wealthers;
  77. }
  78. //OkHttp网络访问:get请求
  79. public static String getByOkhttp(String url) throws Exception {
  80. // 1 OkHttpClient对象
  81. OkHttpClient client=new OkHttpClient();
  82. //2 Request对象
  83. Request request=new Request.Builder().url(url).build();
  84. //3 Request封装为Call
  85. Call call=client.newCall(request);
  86. //4 发送请求,获取响应
  87. Response response = call.execute();
  88. int code=response.code();
  89. String res=response.body().string();
  90. return res;
  91. }
  92. public static Bitmap getOkHttpImage(String str) throws Exception {
  93. OkHttpClient client=new OkHttpClient();
  94. Request request=new Request.Builder().url(str).build();
  95. Response response=client.newCall(request).execute();
  96. InputStream inputStream=response.body().byteStream();
  97. Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
  98. return bitmap;
  99. }
  100. //Gson解析json
  101. // public static Weather parsonGson(String jsonStr) throws Exception {
  102. // JSONObject object=new JSONObject(jsonStr);
  103. //
  104. // JSONObject w=object.getJSONObject("weatherinfo");
  105. //
  106. // Gson gson=new Gson();
  107. //
  108. // Weather weather=gson.fromJson(w.toString(),Weather.class);
  109. //
  110. //
  111. // return weather;
  112. //
  113. // }
  114. }

然后我还是把安卓源码丢上来,说不定哪一天想拿起来改改,hhhhhhhhh.

某盘链接.

链接:https://pan.baidu.com/s/1LdLJcf2ZgIzyDc9HTzfk9g 
提取码:hxr1

软件如下

某盘链接,仅限十人,因为这个请求次数不多,而且这个是用来练练手的项目,随便看看,hhhhhhhh。

链接:https://pan.baidu.com/s/1aQ5nn_GKP-rCe7j9LTNg9A 
提取码:kn8r

以上使用资源接口引用于:

免费天气API接口|天气预报接口|全球天气API接口|气象预警|空气质量 (tianqiapi.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/468029
推荐阅读
相关标签
  

闽ICP备14008679号