解析json数据,获取你需要的信息
首先在manifest中添加允许访问网络的权限信息
<uses-permission android:name="android.permission.INTERNET"/>
Main
package com.chuanxidemo.shaoxin.demo08; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; 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; /** * Created by shaoxin on 2017/2/25. */ public class Main extends AppCompatActivity { private TextView txt; private Button btn2; private BufferedReader bufferedReader; private StringBuffer stringBuffer; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); stringBuffer = new StringBuffer(); txt = (TextView) findViewById(R.id.txt); btn2 = (Button) findViewById(R.id.btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(){ @Override public void run() {//使用线程的目的是防止该过程在主线程中运行 super.run(); getData(); parseJsonData(getData()); } }.start(); //解析 } }); } private void parseJsonData (String string){ //try{jsonp9+?((.+?))}catch(e){} try { //解析的过程就是在逐层剥开代码的过程 JSONObject jsonObject = new JSONObject(string);//每一层都是一个Object对象 Log.v("msg",jsonObject.get("try").toString()); Log.i("msg", "parseJsonData: "+jsonObject.getString("timestamp")); jsonObject.getJSONObject("data"); JSONArray jsonArray = jsonObject.getJSONArray("tvQipuId");//对象中的数组 Log.i("msg", "parseJsonData: "+jsonArray.get(0).toString()); } catch (JSONException e) { e.printStackTrace(); } } private String getData() { try { URL url = new URL("http://cache.video.iqiyi.com/jp/avlist/202861101/1/?callback=jsonp9");//json地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET");//使用get方法接收 InputStream inputStream = connection.getInputStream();//得到一个输入流 bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTf-8")); String sread = null; while ((sread = bufferedReader.readLine()) != null) { stringBuffer.append(sread); stringBuffer.append(" "); } // Log.i("msg", "onClick: " + stringBuffer.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return stringBuffer.toString(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取数据" /> </LinearLayout>