Gson解析数据

package com.bwie.test;

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;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bwie.test.JingDian.ResultBean;
import com.bwie.test.JingDian.ResultBean.RowsBean;
import com.bwie.test.JingDian.ResultBean.RowsBean.InfoBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    private String urlPath="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0";
    private List<RowsBean> rowsBeans=new ArrayList<JingDian.ResultBean.RowsBean>();
    private ListView lv;
   
    
    private Handler handler=new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                //获得数据
                String text=(String) msg.obj;
                tojson(text);
                break;
              
            default:
                break;
            }
        }

        
    };

//-----------------gson解析   注意:需要导入gson.Jar包------------------------------------------------
    
    //解析数据
    private void tojson(String text) {
        Gson gson=new Gson();
        //类名
        JingDian jingDian=gson.fromJson(text, JingDian.class);
        Log.i("kkkkkkkkkk", jingDian.getResult().getRows().get(0).getInfo().getAddress()+"");
        ResultBean resultBean=jingDian.getResult();
        rowsBeans=resultBean.getRows();
        
        //设置适配器             
        MyAdapter adapter=new MyAdapter(rowsBeans, this);
        lv.setAdapter(adapter);                       
        
    };

//-------------------------onCreate中---------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);      
        //获得数据
        huodeshuju();
       
       
    }

//------------------------------------------

    //获得数据
    private void huodeshuju() {
      

        //使用线程
        new Thread(){
                    public void run() {
                        try {
                            //先创建出了一个URL对象,urlPath:是我们访问接口地址
                            URL url=new URL(urlPath);
                            
                             //URL链接对象,通过URL对象打开一个connection链接对像
                            HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
                            //设置urlConnection对象链接超时
                            urlConnection.setConnectTimeout(5000);
                            //设置urlConnection对象获取数据超时
                            urlConnection.setReadTimeout(5000);
                            //设置本次urlConnection请求方式
                            urlConnection.setRequestMethod("GET");
                            //调用urlConnection的链接方法,线程等待,等待的是服务器所给我们返回的结果集
                           // urlConnection.connect();
                            //获取本次网络请求的状态码
                            int code=urlConnection.getResponseCode();
                            //如果本次返回的状态吗是200(成功)
                            if (code==200) {
                                 //调用urlConnection.getInputStream得到本次请求所返回的结果流
                                InputStream inputStream=urlConnection.getInputStream();
                                //创建一个BufferedReader,去读取结果流
                                BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                                String readLine;
                                StringBuffer buffer=new StringBuffer();
                                while ((readLine=reader.readLine())!=null) {
                                    buffer.append(readLine);
                                    
                                }
                                //读取完结果流之后所得到的结果
                                String result=buffer.toString();
                                Log.i("数据:----", result);
                                Message message=new Message();
                                message.what=0;
                                message.obj=result;
                                handler.sendMessage(message);
                                inputStream.close();
                            }
                        } catch (MalformedURLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    };
                }.start();
        
    }


 
    
}

posted on 2016-10-17 13:57  巫山老妖  阅读(1124)  评论(0编辑  收藏  举报