Android -开源框架之XUtils3

Android-开源框架之XUtils3


以下是一个小demo:
首先在清单文件添加权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dzz.android32_xutils">
    <!--添加权限-->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>  <!--网络-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  <!--内存卡   写-->
                <!--配置.MyApplication-->
    <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

还要在build.gradle里 添加以下依赖

    compile 'org.xutils:xutils:3.3.42'

还要新建一个ActivityMyApplication

package com.dzz.android32_xutils;

import android.app.Application;

import org.xutils.x;

/**
 * Created by 朝花偏不夕拾 on 2017/2/28.
 */

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化XUtils
        x.Ext.init(this);

    }
}

demo布局

<?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:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.dzz.android32_xutils.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="admin"
        android:id="@+id/et_main_name"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="123456"
        android:id="@+id/et_main_pwd"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/b_main_login"/>
</LinearLayout>

然后编写登录点击

package com.dzz.android32_xutils;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public class MainActivity extends AppCompatActivity {
    @ViewInject(R.id.et_main_name)
    private EditText et_main_name;

    @ViewInject(R.id.et_main_pwd)
    private EditText et_main_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        x.view().inject(this);
    }
    @Event(value = R.id.b_main_login,type = View.OnClickListener.class)
    private void login(View view){
        //获取文本框的值
        String uname=et_main_name.getText().toString();
        String upwd=et_main_pwd.getText().toString();

        //发送请求
        RequestParams params=new 
                            //地址要换成你的网络IP
        RequestParams("http://192.168.43.237:8090/front/loginAction.xhtml");
        params.addParameter("uname",uname);
        params.addParameter("upass",upwd);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
            }

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

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        //长按
//        @Event(value = R.id.b_main_login,type = View.OnLongClickListener.class)
//        private boolean login(View view){
//            //获取文本框的值
//            String uname=et_main_name.getText().toString();
//            String upwd=et_main_pwd.getText().toString();
//
//            //发送请求
//            RequestParams params=new RequestParams("http://192.168.43.237:8090/front/loginAction.xhtml");
//            params.addParameter("name",uname);
//            params.addParameter("pwd",upwd);
//        x.http().post(params, new Callback.CommonCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//                Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//
//            }
//
//            @Override
//            public void onFinished() {
//
//            }
//        });
//        return true;
    }

}

Eclipse 编写服务器
服务器端在下面云盘:
http://pan.baidu.com/s/1c1NnRv6

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值