网络请求数据

来源:互联网 发布:淘宝好奇的鼻子香水屋 编辑:程序博客网 时间:2024/06/04 18:37
public class NetWorkUtils {
    
    public static String getStr(String path){
        
        HttpClient http=new DefaultHttpClient();
        HttpGet get=new HttpGet(path);
        try {
            HttpResponse response = http.execute(get);
            if(response.getStatusLine().getStatusCode()==200){
                return EntityUtils.toString(response.getEntity(),"gbk");
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

============================================

private void httpData() {
        new AsyncTask<String, Void, String>(){

            
            @Override
            protected String doInBackground(String... params) {
                String json=NetWorkUtils.getStr(params[0]);
                return json;
            }
            protected void onPostExecute(String result) {
                Gson gson=new Gson();
                List<Bean>    bean=gson.fromJson(result,
                        new TypeToken<List<Bean>>(){}.getType());
                for (Bean bean2 : bean) {
                    String type=bean2.type;
                    if("vpimg".equals(type)){
                        vplist=bean2.item;
                    }else if("listmr".equals(type)){
                        list1=bean2.item;
                    }else if("listxl".equals(type)){
                        list2=bean2.item;
                    }
                    else if("listxy".equals(type)){
                        list3=bean2.item;
                    }
                    else if("listjg".equals(type)){
                        list4=bean2.item;
                    }
                    
                    
                }
            };
        }.execute(path);
        
    }

0 0