Android项目遇到的问题及解决思路

Android (45) 2023-12-24 15:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说Android项目遇到的问题及解决思路,希望能够帮助你!!!。

把项目中常用到的一些常量,接口,方法,都可以进行封装

常量:封装

package com.zhidisoft.slefnote.util;

/**

* Created by Administrator on 2016/7/11.

*/

public class Contant {

public static final String IP = "http://192.168.100.3:8080/mymoney/index.do";

public static final String UNIQUE = IP + "?m=user&a=unique";

public static final String REGISTER = IP + "?m=user&a=reg";

}

接口封装:

package com.zhidisoft.slefnote.activity;

import android.app.ProgressDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Toast;

import org.xutils.common.Callback;

import org.xutils.ex.HttpException;

import org.xutils.http.RequestParams;

import org.xutils.x;

import java.util.Map;

import java.util.Set;

public class BaseActivity extends AppCompatActivity implements Callback.CommonCallback {

Callback.Cancelable cancelable = null;

private ProgressDialog dialog = null;

/**

* 展示progressdialog,进度条

*/

public void showDialog() {

if (dialog == null) {

dialog = new ProgressDialog(this);

dialog.setTitle("请稍等");

dialog.setMessage("正在加载");

dialog.show();

}

}

/**

* 取消展示进度条

*/

public void cancleDialog() {

if (dialog != null && dialog.isShowing()) {

dialog.dismiss();

dialog = null;

}

}

public void getData(String url, Map map, boolean isShowDialog) {

RequestParams params = new RequestParams(url);

if (map != null) {

Set keys = map.keySet();

for (String key : keys) {

//存放请求参数

params.addParameter(key, map.get(key));

}

}

if (isShowDialog) {

showDialog();

}

cancelable = x.http().get(params, this);

}

@Override

public void onSuccess(String result) {

}

@Override

public void onError(Throwable ex, boolean isOnCallback) {

cancleDialog();

if (ex instanceof HttpException) { // 网络错误

HttpException httpEx = (HttpException) ex;

int responseCode = httpEx.getCode();

String responseMsg = httpEx.getMessage();

String errorResult = httpEx.getResult();

// ...

} else { // 其他错误

// ...

}

Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();

}

@Override

public void onCancelled(CancelledException cex) {

cancleDialog();

}

@Override

public void onFinished() {

cancleDialog();

}

}

接口应用:

package com.zhidisoft.slefnote.activity;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.text.TextUtils;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import com.zhidisoft.slefnote.R;

import com.zhidisoft.slefnote.util.Contant;

import org.json.JSONException;

import org.json.JSONObject;

import java.util.HashMap;

import java.util.Map;

public class RegisterActivity extends BaseActivity {

private Button btn_register;

private EditText et_username, et_password;

private int REQUEST_UNIQUE = 0;

private int REQUEST_REGISTER = 1;

private int REQUEST_CODE = REQUEST_UNIQUE;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_register);

btn_register = (Button) findViewById(R.id.btn_register);

et_username = (EditText) findViewById(R.id.et_usernmae);

et_password = (EditText) findViewById(R.id.et_password);

btn_register.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

unique();

}

});

}

public void unique() {

REQUEST_CODE = REQUEST_UNIQUE;

String username = et_username.getText().toString().trim();

String pwd = et_password.getText().toString().trim();

if (TextUtils.isEmpty(username) || TextUtils.isEmpty(pwd)) {

Toast.makeText(this, "用户名和密码不可为空", Toast.LENGTH_LONG).show();

return;

}

Map map = new HashMap<>();

map.put("username", username);

getData(Contant.UNIQUE, map, true);

}

public void register() {

REQUEST_CODE = REQUEST_REGISTER;

String username = et_username.getText().toString().trim();

String pwd = et_password.getText().toString().trim();

Map map = new HashMap<>();

map.put("username", username);

map.put("password", pwd);

getData(Contant.REGISTER, map, false);

}

@Override

public void onSuccess(String result) {

super.onSuccess(result);

if (REQUEST_CODE == REQUEST_UNIQUE) {//如果请求是判断唯一性

parseUnique(result);

} else if (REQUEST_CODE == REQUEST_REGISTER) {//如果请求是注册

parseRegister(result);

}

}

public void parseUnique(String result) {

try {

JSONObject obj = new JSONObject(result);

int code = obj.getInt("code");

if (code == 1) {//如果唯一进行网络访问进行注册

register();

} else {

Toast.makeText(this, obj.getString("msg"), Toast.LENGTH_SHORT).show();

cancleDialog();

}

} catch (JSONException e) {

e.printStackTrace();

cancleDialog();

}

}

public void parseRegister(String result) {

cancleDialog();

try {

JSONObject obj = new JSONObject(result);

int code = obj.getInt("code");

if (code == 1) {

Toast.makeText(this, "注册成功,请登录", Toast.LENGTH_SHORT).show();

this.finish();

} else {

Toast.makeText(this, obj.getString("msg"), Toast.LENGTH_SHORT).show();

}

} catch (JSONException e) {

e.printStackTrace();

}

}

}

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

发表回复