HttpUrlConnection+JSON
Json之前,大家都用 XML 传递数据。XML 是一种纯文本格式,所以适合在网络上交换数据,但是 XML 格式比较复杂,终于道格拉斯·克罗克福特(Douglas Crockford)发明了JSON 这种超轻量级的数据交换格式。
任何把 JavaScript 变成 Json ,就是把这个对象序列化为Json字符串,然后才可以通过网络传递; 收到一个Json格式的字符串,
如果我们收到一个JSON格式的字符串,只需要把它反序列化成一个JavaScript对象,就可以在JavaScript中直接使用这个对象了。
天气预报
package com.lenovo.zy.tanqi;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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 TAG = "MainActivity";
private String weatherAPI = "https://free-api.heweather.com/s6/weather/now?key=231395b53ae94e9897585818a7c3e63b&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 view) {
String city = cityET.getText().toString();
new MyTask().execute(weatherAPI + city);
}
});
}
private void bindID() {
cityET = findViewById(R.id.city_et);
searchBtn = findViewById(R.id.search_btn);
tempTV = findViewById(R.id.temp_tv);
weatherTV = findViewById(R.id.weather_tv);
windTV = findViewById(R.id.wind_tv);
}
class MyTask extends AsyncTask<String, String, String> {
@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_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 {
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 temperrature = nowObj.getString("tmp");
// Log.e(TAG, "onPostExecute: "+ weather+wind+"+++++++++++++++++++++++++++++++++++++=");
weatherTV.setText(weather);
windTV.setText(wind);
tempTV.setText(temperrature);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}