HttpUrlConnection+JSON应用实例
获取天气实况
xml代码
<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.lianxiti.WeatherActivity">
<EditText
android:id="@+id/weather_et"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/weather_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="开始查询"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:text="天气:"
android:textSize="30sp"
android:gravity="center"/>
<TextView
android:id="@+id/weather_tv1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:text="风力:"
android:textSize="30sp"
android:gravity="center"/>
<TextView
android:id="@+id/weather_tv2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:text="温度:"
android:textSize="30sp"
android:gravity="center"/>
<TextView
android:id="@+id/weather_tv3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp"/>
</LinearLayout>
</LinearLayout>
java代码
public class WeatherActivity extends AppCompatActivity {
private EditText weaEt;
private Button weaBtn;
private TextView weaTv1;
private TextView weaTv2;
private TextView weaTv3;
private String weatherAPI = "https://free-api.heweather.com/s6/weather/now?key=1318c609832f407dab8f64b44d6d9160&location=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
bind();
weaBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String city = weaEt.getText().toString();
new WeaTask(WeatherActivity.this).execute(weatherAPI+city);
}
});
}
class WeaTask extends AsyncTask<String,String,String>{
private Context context;
public WeaTask(Context context){
this.context = context;
}
@Override
protected String doInBackground(String... strings) {
StringBuffer stringBuffer = new StringBuffer();
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_fail";
}
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_fail")){
Toast.makeText(WeatherActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
}else {
try {
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");
weaTv1.setText(weather);
weaTv2.setText(wind);
weaTv3.setText(temperature);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void bind() {
weaEt = findViewById(R.id.weather_et);
weaBtn = findViewById(R.id.weather_btn);
weaTv1 = findViewById(R.id.weather_tv1);
weaTv2 = findViewById(R.id.weather_tv2);
weaTv3 = findViewById(R.id.weather_tv3);
}
}