图片

原创 2018年04月15日 20:22:02
package zhanghaijiao.bawei.com.my1601r;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;


import com.google.gson.Gson;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


import java.util.List;


import zhanghaijiao.bawei.com.my1601r.bean.EnjoyResultData;


/**
 * Created by jane on 2018/4/13.
 */


public class EnjoyFragment extends Fragment {


    private GridView gridView;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View v=inflater.inflate(R.layout.enjoy,container,false);
        gridView = v.findViewById(R.id.gv);


        return v;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        if(NetTypeUtils.isConn(getActivity())){
            //获取网络数据
            MyTask myTask=new MyTask();
            myTask.execute("http://120.27.23.105/product/getProductCatagory?cid=1");


        }else{
            NetTypeUtils.openNetDialog(getActivity());
        }




    }


    class MyTask extends AsyncTask<String,Void,String>{


        @Override
        protected String doInBackground(String... strings) {


            try {
                HttpClient httpClient=new DefaultHttpClient();
                HttpGet httpGet=new HttpGet(strings[0]);
                //3.执行
                HttpResponse response = httpClient.execute(httpGet);
                //4.判断结果码
                if(response.getStatusLine().getStatusCode()==200){
                    //5.得到结果
                    HttpEntity entity = response.getEntity();
                    String s = EntityUtils.toString(entity);
                    return s;
                }




            } catch (Exception e) {
                e.printStackTrace();
            }




            return null;
        }


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);


            Gson gson=new Gson();
            EnjoyResultData enjoyResultData = gson.fromJson(s, EnjoyResultData.class);
            //选取数据
            List<EnjoyResultData.DataBean.ListBean> list = enjoyResultData.getData().get(0).getList();


            GridViewAdapter adapter=new GridViewAdapter(getActivity(),list);
            gridView.setAdapter(adapter);










        }
    }
}
收藏助手
不良信息举报
您举报文章:图片
举报原因:
原因补充:

(最多只允许输入30个字)