简单的天气预报小软件

简单的天气预报小软件package com example pc tqyb import android os AsyncTask import android support v7 app AppCompatAct import android os Bundle import android view View import android widget

大家好,我是讯享网,很高兴认识大家。
package com.example.pc.tqyb; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends AppCompatActivity { 
    private TextView weatherTV; private TextView windTV; private TextView tempTV; private Button searchBtn; private EditText cityET; private String weatherAPI = "https://free-api.heweather.com/s6/weather/now?key=5fb21da829bd1ec5ba9ebd6d1&location="; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindID(); searchBtn.setOnClickListener(new View.OnClickListener() { 
  
    
  //监听 @Override public void onClick(View v) { String city = cityET.getText().toString(); new MyTask().execute(weatherAPI+city); } }); } class MyTask extends AsyncTask<String,String,String>{ StringBuffer stringBuffer = new StringBuffer(); @Override protected String doInBackground(String... strings) { try { URL url = new URL(strings[0]); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream inputStream = null; if (connection.getResponseCode()==200){ 
  
    
  //判断是否连接 inputStream = connection.getInputStream(); }else { return "network_failed"; } InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader); String temp = ""; while ((temp=bufferedReader.readLine())!=null){ stringBuffer.append(temp); } bufferedReader.close(); reader.close(); inputStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return stringBuffer.toString(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s.equals("network_failed")){ Toast.makeText(MainActivity.this,"网络失败",Toast.LENGTH_SHORT).show(); }else { try { //JSON解析 JSONObject object = new JSONObject(s); JSONArray array = object.getJSONArray("HeWeather6"); JSONObject object1 = array.getJSONObject(0); JSONObject nowobj = object1.getJSONObject("now"); String weather = nowobj.getString("cond_txt"); String wind = nowobj.getString("wind_dir")+nowobj.getString("wind_sc")+"级"; String temperature = nowobj.getString("tmp"); weatherTV.setText(weather); windTV.setText(wind); tempTV.setText(temperature); } catch (JSONException e) { e.printStackTrace(); } } } } private void bindID() { weatherTV = findViewById(R.id.wearth_tv); windTV = findViewById(R.id.wind_tv); tempTV = findViewById(R.id.temp_tv); searchBtn = findViewById(R.id.search_btn); cityET = findViewById(R.id.city_et); } } 

讯享网
讯享网<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.pc.tqyb.MainActivity"> <EditText  android:id="@+id/city_et" android:layout_width="match_parent" android:layout_height="50dp" android:text="" android:textSize="20sp"/> <Button  android:id="@+id/search_btn" android:layout_width="match_parent" android:layout_height="50dp" android:text="查询"/> <TextView  android:id="@+id/wearth_tv" android:layout_width="match_parent" android:layout_height="50dp" /> <TextView  android:id="@+id/wind_tv" android:layout_width="match_parent" android:layout_height="50dp" /> <TextView  android:id="@+id/temp_tv" android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout> 

这里写图片描述
讯享网

小讯
上一篇 2025-02-28 18:54
下一篇 2025-02-20 12:45

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/75695.html