您的位置:首页 > 理论基础 > 计算机网络

和风天气 (简易版)

2018-03-15 13:33 309 查看
做一个简单的天气预报,首先想的就是联网权限,还有对“和风天气” 服务器的HTTP请求

<uses-permission android:name="android.permission.INTERNET" />


先在和风天气(百度搜索)上注册一个账号 并且激活然后登陆,在“官网里”





在控制台里 获取自己的 key



获取JSON数据 的结构

“网址”+”?key”+”自己的秘钥”+”&location=”

接下里我们就需要在xml里 布局了

<?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.myapplication13.MainActivity">

<EditText
android:hint="请输入城市名称"
android:id="@+id/main_editView"
android:layout_width="match_parent"
android:layout_height="50dp" />

<Button
android:id="@+id/main_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="查询" />

<RelativeLayout
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="40dp">

<TextView
android:id="@+id/main11"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="天气:"
android:textSize="25sp" />

<TextView
android:id="@+id/main_tianqi"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="@id/main11"
android:textSize="25dp" />

</RelativeLayout>

<RelativeLayout
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="40dp">

<TextView
android:id="@+id/main22"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="温度:"
android:textSize="25sp" />

<TextView
android:id="@+id/main_wendu"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="@id/main22"
android:textSize="25dp" />

</RelativeLayout>

<RelativeLayout
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="40dp">

<TextView
android:id="@+id/main33"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="风力:"
android:textSize="25sp" />

<TextView
android:id="@+id/main_fengli"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="@id/main33"
```

android:textSize="25dp" />

</RelativeLayout>

</LinearLayout>


实时效果praview:



在java代码里 我们用AsynTask 来对HTTPURLConnection 和JSONObject 进行了属性 返回 传值 调用

package com.example.myapplication13;

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.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 EditText editText;
private Button button;
private TextView tv_tianqi;
private TextView tv_wendu;
private TextView tv_fengli;
private String API = "https://free-api.heweather.com/s6/weather/now?key=11e895a6b3854f0fb49508eea65df6ca&location=";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binID();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String city = editText.getText().toString();
new MyWeather().execute(API + city);

}
});
}

class MyWeather extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
StringBuffer stringBuffer = null;

try {
URL url = new URL(strings[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = null;
if (httpURLConnection.getResponseCode() == 200) {
inputStream = httpURLConnection.getInputStream();
//检测网络异常
} else {
return "11";
}
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(reader);
stringBuffer = new StringBuffer();
String timp = null;
while ((timp = bufferedReader.readLine()) != null) {
stringBuffer.append(timp);
}
inputStream.close();
reader.close();
bufferedReader.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("11")) {

Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
}
try {
JSONObject object = new JSONObject(s);
JSONObject object1 = object.getJSONArray("HeWeather6").getJSONObject(0);
JSONObject basic = object1.getJSONObject("basic");
JSONObject now = object1.getJSONObject("now");
String city = basic.getString("location");
String tianqi = now.getString("cond_txt");
String wendu = now.getString("tmp");
String fengli = now.getString("wind_dir");
String qiangdu = now.getString("wind_sc");
tv_tianqi.setText("今天是" + "“" + tianqi + "”" + "哦,主人");
tv_wendu.setText(wendu + "℃");
tv_fengli.setText(fengli + qiangdu + "级");

} catch (JSONException e) {
e.printStackTrace();
}

}

}

private void binID() {
editText = findViewById(R.id.main_editView);
button = findViewById(R.id.main_button);
tv_tianqi = findViewById(R.id.main_tianqi);
tv_fengli = findViewById(R.id.main_fengli);
tv_wendu = findViewById(R.id.main_wendu);
}
}


运行后 简单的天气软件就做出来啦。 不过我的模拟器问题。只能由拼音代替城市喽

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HTTPURLconnection JSON