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

简单的事件逻辑:

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

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

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

安卓简单的应用——天气预报_第1张图片

package com.example.weather_forecast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    Button button;
    TextView tishi;
    ListView listView;
    SharedPreferences spfcode,spf;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDate();
        chaxun();
        moren();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tempcity = editText.getText().toString();
                wealthinit(spfcode.getString(tempcity,"101010100"));
                tishi.setText("当前查询城市:"+ tempcity);
                if(!spfcode.getString(tempcity,"").equals("")){
                    SharedPreferences.Editor editor = spf.edit();
                    editor.putString("1",tempcity);
                    editor.commit();
                }
            }
        });
    }

    private void moren() {
        if(spf.getString(1+"","").equals("")){
            SharedPreferences.Editor editor = spf.edit();
            editor.putString("1","北京");
            editor.commit();
        }
        wealthinit(spfcode.getString(spf.getString("1","北京"),"101010100"));
    }

    private void initDate() {
        editText = findViewById(R.id.per_gaichenshi);
        button = findViewById(R.id.per_citytijiao);
        tishi = findViewById(R.id.tishi);
        listView = findViewById(R.id.per_list);

        spfcode = getSharedPreferences("Citycode",MODE_PRIVATE);
        spf = getSharedPreferences("Personal",MODE_PRIVATE);
    }
    class MyTask extends AsyncTask {


        //获取json数据
        @Override
        protected String doInBackground(String... strings) {

            String jsonStr= null;
            try {
                jsonStr = Httputils.getJson(strings[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(jsonStr);

            return jsonStr;
        }
        //解析json数据,显示出来
        @Override
        protected void onPostExecute(String s) {
            try {
                Wealther[] wealthers = Httputils.parseJson(s);
                List list = Arrays.asList(wealthers);
                Tianqiadapt adapter = new Tianqiadapt(MainActivity.this, list);
                listView.setAdapter(adapter);

                int[] highTemp = new int[list.size()];
                int[] lowTemp = new int[list.size()];
                for(int i=0;i

软件源码

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

package com.example.weather_forecast;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


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

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Httputils {

    public static Bitmap getImage(String url) throws Exception {
        Bitmap bitmap=null;
        URL a=new URL(url);

        HttpURLConnection connection= (HttpURLConnection) a.openConnection();

        int statusCode=connection.getResponseCode();
        if(statusCode==HttpURLConnection.HTTP_OK){
            InputStream inputStream=connection.getInputStream();
            bitmap= BitmapFactory.decodeStream(inputStream);
            inputStream.close();
            connection.disconnect();
        }
        return bitmap;

    }
    //获取json数据
    public static String getJson(String url) throws Exception {

        URL a=new URL(url);

        HttpURLConnection connection= (HttpURLConnection) a.openConnection();
        int statusCode=connection.getResponseCode();
        if(statusCode==HttpURLConnection.HTTP_OK){
            InputStream inputStream=connection.getInputStream();
            StringBuffer sb=new StringBuffer();
            byte[] buff=new byte[1024];
            int len;
            while((len=inputStream.read(buff))!=-1){
                sb.append(new String(buff,0,len));
            }

            inputStream.close();
            connection.disconnect();

            return sb.toString();
        }

        return null;
    }

    //解析json数据
    public static Wealther[] parseJson(String jsonStr) throws Exception {
        Wealther[] wealthers = new Wealther[7];
        for(int i=0;i

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

某盘链接.

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

软件如下

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

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

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

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

你可能感兴趣的:(android,android,studio,java)