欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

xUtils框架网络获取

程序员文章站 2022-04-19 10:29:26
...

1.导包和添加权限

xUtils框架网络获取


<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>




2.自己创个类继承Application

public class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);//初始化xutils
        x.Ext.setDebug(BuildConfig.DEBUG);//是否输出debug日志,开启debug会影响性能

    }
}




3.在AndroidManifest里面使用

xUtils框架网络获取




4.主函数

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.get_btn:
                getData();
                break;
            case R.id.post_btn:
                postData();
                break;
        }

    }
    //使用post请求获取网络数据
    private void postData() {
        String path = "http://218.244.149.129:9010/api/companylist.php";
        RequestParams params = new RequestParams(path);
        params.addBodyParameter("industryid", "98");

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("sxl", "onSuccess: "+result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                Log.d("sxl", "onFinished: ");
            }
        });
        
        
        
    }
    //使用get请求获取网络数据
    public void getData() {
        String path = "http://218.244.149.129:9010/api/industry.php";
        RequestParams params = new RequestParams(path);
        Callback.Cancelable cancelable = x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                // 该方法   是 成功 返回值
                Log.d("TAG", "onSuccess: " + Thread.currentThread().getId() + "____s" + result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                // 错误的时候
                Log.d("TAG", "onError: ");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                // 取消的时候
                Log.d("TAG", "onCancelled: ");
            }

            @Override
            public void onFinished() {
                //结束的时候     该方法 每次请求后 必然 执行的一个方法
                Log.d("TAG", "onFinished: ");
            }
        });
        cancelable.cancel();//取消此次网络访问
    }
}





5.listview适配器

public class MyAdapter extends BaseAdapter {
    private ArrayList<Data> list;
    private Context mContext;
    private LayoutInflater mInflater;

    public MyAdapter(ArrayList<Data> list, Context context) {
        this.list = list;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHoler vh = null;
        if(view==null){
            vh = new ViewHoler();
            view = mInflater.inflate(R.layout.item,viewGroup,false);
            vh.pic_img = (ImageView) view.findViewById(R.id.img);
            vh.tv = (TextView) view.findViewById(R.id.title_tv);
            view.setTag(vh);
        }else {
            vh = (ViewHoler) view.getTag();
        }
        Data data = (Data) getItem(i);
        //TODO 设置
        return view;
    }

    class ViewHoler{
        ImageView pic_img;
        TextView tv;
    }
}




6.主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bw.alice.aday17xutils.MainActivity">
   <Button
       android:id="@+id/get_btn"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:onClick="onClick"
       android:text="get请求"/>

   <Button
       android:id="@+id/post_btn"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:onClick="onClick"
       android:text="post请求"/>

   <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/lv"></ListView>
</LinearLayout>




7.item布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bw.alice.aday17xutils.MainActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="5dp"/>

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/img"
        android:textSize="20sp"/>

</RelativeLayout>


相关标签: android 手机