Git Product home page Git Product logo

novahttp's Introduction

NovaHttp

一个我现在爱不释手的网络请求封装思路。

现在你的网络请求只要如此

1.标注出你的参数

/**
 * url
 */
@NovaUrl
final String URL = Constants.BASE_URL.concat("&c=user&a=resetPassword");
/**
 * request
 */
@NovaRequest(name = "old_password")
String reqOldPsw;
@NovaRequest(name = "new_password")
String reqNewPsw;
@NovaRequest(name = "user_id")
String reqUserID;

/**
 * response
 */
@NovaResponse(mama = "code")
String repCode;
@NovaResponse(mama = "msg")
String repMsg;

2.继承,绑定

3.Go

下面是一个栗子

package com.lhave.yuebar.ui.activity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alipay.sdk.encrypt.MD5;
import com.lhave.yuebar.APP;
import com.lhave.yuebar.R;
import com.lhave.yuebar.data.business.Constants;
import com.lhave.yuebar.data.novahttpv3.INetDream;
import com.lhave.yuebar.data.novahttpv3.MasterFu;
import com.lhave.yuebar.data.novahttpv3.annotation.NovaRequest;
import com.lhave.yuebar.data.novahttpv3.annotation.NovaResponse;
import com.lhave.yuebar.data.novahttpv3.annotation.NovaUrl;
import com.lhave.yuebar.ui.base.BaseActivity;
import com.lhave.yuebar.utils.LogUtils;
import com.sea_monster.common.Md5;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by gongjiangpeng on 15/10/23.
 */
public class ChangePSWActivity extends BaseActivity implements INetDream{

@Bind(R.id.old_psw)
EditText mEdtOldPsw;
@Bind(R.id.new_psw)
EditText mEdtNewPsw;
@Bind(R.id.new_psw_again)
EditText mEdtNewPswAgain;

MasterFu mMasterFu;

/**
 * url
 */
@NovaUrl
final String URL = Constants.BASE_URL.concat("&c=user&a=resetPassword");

/**
 * request
 */
@NovaRequest(name = "old_password")
String reqOldPsw;
@NovaRequest(name = "new_password")
String reqNewPsw;
@NovaRequest(name = "user_id")
String reqUserID;

/**
 * response
 */
@NovaResponse(mama = "code")
String repCode;
@NovaResponse(mama = "msg")
String repMsg;



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

    ButterKnife.bind(this);

    initTitle();

    mMasterFu = MasterFu.create();
    mMasterFu.bind(this);
}

private void initTitle() {
    setTitle(R.string.change_psw);
}

@OnClick({R.id.see_psw, R.id.see_new_psw, R.id.see_new_psw_again})
public void onShowPSWClicked(View view){
    switch (view.getId()){
        case R.id.see_psw:
            mEdtOldPsw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            break;
        case R.id.see_new_psw:
            mEdtNewPsw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            break;
        case R.id.see_new_psw_again:
            mEdtNewPswAgain.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            break;
    }
}

@OnClick(R.id.right)
public void onCommitClicked(View view){
    String newPsw = mEdtNewPsw.getText().toString();
    String newPswAgain = mEdtNewPswAgain.getText().toString();

    if(TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(newPswAgain) || TextUtils.isEmpty(mEdtOldPsw.getText().toString())){
        LogUtils.toast(this, R.string.empty_password);
        return;
    }

    if( newPsw.equals(newPswAgain) ){
        mMasterFu.go();
    }else{
        LogUtils.toast(this, R.string.two_psw_not_same);
    }
}


@Override
public void pre(int requestID) {
    reqOldPsw = Md5.encode(mEdtOldPsw.getText().toString());
    reqNewPsw = Md5.encode(mEdtNewPsw.getText().toString());
    reqUserID = APP.getInstance().getCurrentUser().userID;
}

@Override
public void done(int requestID, String result) {
    if( Constants.CODE_SUCCESS.equals(repCode) ){
        LogUtils.toast(this, R.string.change_psw_success);
        APP.getInstance().getCurrentUser().password = reqNewPsw;
        APP.getInstance().updateCurrentUser();
        finish();
    }else{
        LogUtils.toast(this, repMsg);
    }
}

@Override
public void error(int requestID, String errMsg) {
    LogUtils.toast(this, errMsg);
}
}

使用说明(待补)

novahttp's People

Contributors

h2oiswater avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.