當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
android 解析网络数据(JSON)
生活随笔
收集整理的這篇文章主要介紹了
android 解析网络数据(JSON)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解析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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {new Thread(){@Overridepublic 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("\r\n");} // 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"><TextViewandroid:id="@+id/txt"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獲取數據" /> </LinearLayout>?
轉載于:https://www.cnblogs.com/ShaoXin/p/6441943.html
總結
以上是生活随笔為你收集整理的android 解析网络数据(JSON)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery中json数据转换为字典
- 下一篇: ascll码表 ASCALL码表