Android开发之Handler+HttpClient请求解析数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发之Handler+HttpClient请求解析数据相关的知识,希望对你有一定的参考价值。

读取字符流封装类

package com.fan.listview_show.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamUtils {
    
    public static String streamUtils(InputStream inputStream) throws Exception{
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=inputStream.read(buffer))!=-1){
            byteArrayOutputStream.write(buffer, 0, len);
        }
        return byteArrayOutputStream.toString();
    }

}

 

public class Itemtwo extends Activity implements IXListViewListener {
    String url = "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 XListView listView;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            String json = (String) msg.obj;
            Gson gson = new Gson();
            Jsondata jsondata = gson.fromJson(json, Jsondata.class);
            listView.setAdapter(new MyListAdapter(getApplicationContext(),
                    jsondata));
        };

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.itemactivity);
        listView = (XListView) findViewById(R.id.xlistview);
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);
        gethttp();

    }

    private void gethttp() {
        // TODO Auto-generated method stub

        new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url);

                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    int statusCode = httpResponse.getStatusLine()
                            .getStatusCode();
                    if (statusCode == 200) {
                        InputStream inputStream = httpResponse.getEntity()
                                .getContent();
                        String str = StreamUtils.streamUtils(inputStream);
                        Log.i("TAG", str.toString());
                        Message msg = Message.obtain();
                        msg.obj = str;
                        handler.sendMessage(msg);
                    }

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }.start();
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        gethttp();
        onLoad();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        gethttp();
        onLoad();
    }

    private void onLoad() {
        // TODO Auto-generated method stub
        listView.stopRefresh();
        listView.stopLoadMore();
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前系统时间
        String nowTime = df.format(new Date(System.currentTimeMillis()));
        // 释放时提示正在刷新时的当前时间
        listView.setRefreshTime(nowTime);
    }
}

 

以上是关于Android开发之Handler+HttpClient请求解析数据的主要内容,如果未能解决你的问题,请参考以下文章

Android之Handler消息传递机制详解zz

Android开发之Handler+HttpClient请求解析数据

Android--多线程之Handler

Android--多线程之Handler

Android之Handler用法总结

Android--多线程之Handler 前言