JSON是一种数据交换格式,可以使用获取网上数据来解析。
赋予联网权限
<uses-permission android:name="android.permission.INTERNET"/>MainActivity:
package com.example.abc.app2; 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 EditText editText; private Button btn; private TextView tv1; private TextView tv2; private TextView tv3; private String tianqi="https://free-api.heweather.com/s6/weather/now?key=9b019ad89b3f48788d68b26fb4917989&location="; //天气地址将地区用EditTexts输入的内容来改变 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindID(); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Task task=new Task(); task.execute(tianqi+editText.getText().toString()); //启动task完整访问地址=天气+EditText转换成的字符串 } }); } private void bindID() { editText=findViewById(R.id.main_edi); btn=findViewById(R.id.main_btn); tv1=findViewById(R.id.main_tv1); tv2=findViewById(R.id.main_tv2); tv3=findViewById(R.id.main_tv3); } class Task extends AsyncTask<String,String,String>{ @Override protected String doInBackground(String... strings) { StringBuffer stringBuffer=new StringBuffer();//可改动的String try { URL url=new URL(strings[0]); 创建URL对象 HttpURLConnection connection= (HttpURLConnection) url.openConnection(); //用openConnection获取HttpURLConnection对象 InputStream inputStream=null; if (connection.getResponseCode()==200){ //判断是否为200(200为正常值,404为客户端错误,505为服务器错误) inputStream=connection.getInputStream(); }else { return "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();//返回JOSN数据 } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s.equals("fail")){ Toast.makeText(MainActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show(); }else { //解析JSON数据 try { JSONObject obj1=new JSONObject(s); //单条JOSN数据 JSONArray array=obj1.getJSONArray("HeWeather6"); //多条JOSN数据 JSONObject obj2=array.getJSONObject(0); JSONObject obj3=obj2.getJSONObject("now"); String cond=obj3.getString("cond_txt"); String tem=obj3.getString("tmp"); String wind=obj3.getString("wind_dir")+obj3.getString("wind_sc")+"级"; tv1.setText(cond);//给Textview赋值 tv2.setText(tem); tv3.setText(wind); } catch (JSONException e) { e.printStackTrace(); } } } } }效果展示:
