Json轉(zhuǎn)對象的簡單例子

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //轉(zhuǎn)圈
        progressDialog = new ProgressDialog(this);
        progressDialog.show();
        
        //開啟子線程請求數(shù)據(jù)
        new Thread(){
            @Override
            public void run() {
                
                try {
                    //請求數(shù)據(jù)
                    URL url = new URL(path);
                    //httpurlConnection 
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    //設(shè)置請求信息
                    con.setRequestMethod("GET");
                    con.setConnectTimeout(timeout);
                    //得到請求的狀態(tài)碼
                    int status = con.getResponseCode();
                    //得到輸入流
                    InputStream inputStream = con.getInputStream();
                    //創(chuàng)建輸出流
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    //OK
                    if (status == 200) {
                    
                        int len = 0;
                        byte[] buffer = new byte[1024];
                        //循環(huán)獲取數(shù)據(jù)
                        while((len=inputStream.read(buffer))>0) {
                            os.write(buffer, 0, len);
                        }
                        //轉(zhuǎn)化數(shù)據(jù)類型
                        String data = os.toString();
                        //打印數(shù)據(jù)

//                      
//                      //轉(zhuǎn)化數(shù)據(jù)格式
                        JSONObject ob = new JSONObject(data);
                        JSONArray arr = ob.getJSONArray("datas");
                        System.out.println(ob.get("total").toString());
                        Log.e("datta", ob.get("total").toString()+"=========================================");
                        Log.e("haha", ob.toString()+"==============================================");
                        
                        
                    }
                    //關(guān)閉流
                    os.close();
                    inputStream.close();
                    //停止
                    progressDialog.dismiss();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
//                  e.printStackTrace();
                    //停止
                    progressDialog.dismiss();
                    
                    Log.e("出錯(cuò)了", e.getMessage()+"error11111");
                    
                    
                }
            };
            
        }.start();
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容