简单的事件逻辑:
从网上拿去天气数据,展示
然后做了一个数据清洗,然后还保存了一下最后的状态,以至于最后的时候可以打开所见即所得。
至于背景啥的,拿到源码以后自己改。
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<String,Void,String> {
//获取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<Wealther> 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<list.size();i++) {
Wealther temp = list.get(i);
highTemp[i] = Integer.parseInt(temp.getTem_1());
lowTemp[i] = Integer.parseInt(temp.getTem_2());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void wealthinit(String citycode) {
try {
new MyTask().execute("https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid="+citycode);
// System.out.println(tempwealth);
} catch (Exception e) {
e.printStackTrace();
}
}
private void chaxun(){
InputStream is=getResources().openRawResource(R.raw.temp);
InputStreamReader isr= null;//包装成IO流
try {
isr = new InputStreamReader(is,"UTF-8");
BufferedReader bfr=new BufferedReader(isr); //包装成内存流
String instring="";
SharedPreferences.Editor editor = spfcode.edit();
while((instring=bfr.readLine())!=null) {
String[] split = instring.split("=");
if(split.length==2){
editor.putString(split[1],split[0]);
editor.commit();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
软件源码
这一段是下载的,至于其他的嘛似乎不是很重要,随便整整也能出来。
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<wealthers.length;i++){
wealthers[i] = new Wealther();
}
JSONObject object=new JSONObject(jsonStr);
wealthers[0].setUpdate(object.get("update_time").toString());
wealthers[0].setCity(object.get("city").toString());
JSONArray objects = object.getJSONArray("data");
for(int i=0;i<objects.length();i++){
JSONObject temp = objects.getJSONObject(i);
if(i!=0){
wealthers[i].setUpdate(wealthers[0].getUpdate());
wealthers[i].setCity(wealthers[0].getCity());
}
wealthers[i].setDate(temp.get("day").toString());
wealthers[i].setWealth(temp.getString("wea"));
wealthers[i].setTem_1(temp.getString("tem"));
wealthers[i].setTem_2(temp.getString("tem1"));
wealthers[i].setTem_3(temp.getString("tem2"));
wealthers[i].setShidu(temp.getString("humidity"));
wealthers[i].setFenxiang(temp.getString("win"));
wealthers[i].setSunup(temp.getString("sunrise"));
wealthers[i].setSundown(temp.getString("sunset"));
wealthers[i].setMiaoshu(temp.getString("narrative"));
wealthers[i].setMoonup(temp.getString("moonrise"));
wealthers[i].setMoondown(temp.getString("moonset"));
wealthers[i].setZiwaixian(temp.getString("uvDescription"));
}
return wealthers;
}
//OkHttp网络访问:get请求
public static String getByOkhttp(String url) throws Exception {
// 1 OkHttpClient对象
OkHttpClient client=new OkHttpClient();
//2 Request对象
Request request=new Request.Builder().url(url).build();
//3 Request封装为Call
Call call=client.newCall(request);
//4 发送请求,获取响应
Response response = call.execute();
int code=response.code();
String res=response.body().string();
return res;
}
public static Bitmap getOkHttpImage(String str) throws Exception {
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url(str).build();
Response response=client.newCall(request).execute();
InputStream inputStream=response.body().byteStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
return bitmap;
}
//Gson解析json
// public static Weather parsonGson(String jsonStr) throws Exception {
// JSONObject object=new JSONObject(jsonStr);
//
// JSONObject w=object.getJSONObject("weatherinfo");
//
// Gson gson=new Gson();
//
// Weather weather=gson.fromJson(w.toString(),Weather.class);
//
//
// return weather;
//
// }
}
然后我还是把安卓源码丢上来,说不定哪一天想拿起来改改,hhhhhhhhh.
某盘链接.
链接:https://pan.baidu.com/s/1LdLJcf2ZgIzyDc9HTzfk9g
提取码:hxr1
软件如下
某盘链接,仅限十人,因为这个请求次数不多,而且这个是用来练练手的项目,随便看看,hhhhhhhh。
链接:https://pan.baidu.com/s/1aQ5nn_GKP-rCe7j9LTNg9A
提取码:kn8r
以上使用资源接口引用于: