HttpManager.java 24 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
package com.hjx.personalcenter.http;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hjx.personalcenter.R;
import com.hjx.personalcenter.activity.LoginAndRegisterActivity;
import com.hjx.personalcenter.activity.TheStartPageActivity;
import com.hjx.personalcenter.db.SaveParam;
import com.hjx.personalcenter.gson.GsonTool;
import com.hjx.personalcenter.model.CityInfo;
import com.hjx.personalcenter.model.CountyInfo;
import com.hjx.personalcenter.model.ProvinceInfo;
import com.hjx.personalcenter.util.DialogPermission;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class HttpManager {
    private static HttpManager instance;
    private ProgressDialog mProgress = null;

    public static HttpManager getInstance() {
        if (instance == null) {
            instance = new HttpManager();
        }
        return instance;
    }


    //登录接口

    public void login(final String username, final String password, final Context mContext) {
        mProgress = DialogPermission.showProgress(mContext, null, "正在登录...",
                false, true, null);
        HttpClient.getInstance().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        HttpClient.getInstance().get(HttpUrl.loginUrl + "?username=" + username + "&password=" + password, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {

                String str = new String(arg2);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(new String(arg2));
                    String status = jsonObject.getString("status");
                    if (status.equals("100")) {
                        String access_token = jsonObject.getString("access_token");
                        String userId = jsonObject.getString("userId");
//                        //登录成功,保存登录数据并且获取个人信息
                        saveLoginInfo(mContext, username, password, access_token, "true", userId);
                        //HttpManager.getInstance().getuserinfo(username,mContext);

                    } else if (status.equals("200")) {
                        closeProgress();
                        Toast.makeText(mContext, "用户名不存在!", Toast.LENGTH_LONG).show();
                        return;
                    } else if (status.equals("204")) {
                        closeProgress();
                        Toast.makeText(mContext, "密码错误!", Toast.LENGTH_LONG).show();
                    } else {
                        closeProgress();
                        Toast.makeText(mContext, "登录失败!请检查网络", Toast.LENGTH_LONG).show();


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


            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                closeProgress();
                Toast.makeText(mContext, "请检查网络。。" + arg3, Toast.LENGTH_LONG).show();
            }
        });
    }

    //注册接口
    public void register(final Context context, String username, String password, String smscode, String source, final Handler handler) {
        mProgress = DialogPermission.showProgress(context, null, "正在注册...",
                false, true, null);
        JSONObject jsonObject = new JSONObject();
        ByteArrayEntity entity = null;
        try {
            jsonObject.put(HttpKey.USERNAME, username);
            jsonObject.put(HttpKey.PASSWORD, password);
            jsonObject.put(HttpKey.SMSCODE, smscode);
            jsonObject.put(HttpKey.SOURCE, source);
            Log.e("test", "jsonObject" + jsonObject);
            entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpClient.getInstance().addHeader("Accept", "*/*");
        HttpClient.getInstance().post(context, HttpUrl.registeredUrl, entity, "application/json", new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                closeProgress();
                Log.e("test", "onSuccess" + response);
                Message msg = Message.obtain();
                msg.what = HttpCode.REGISTERED_SUCESS;
                msg.obj = response;
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                closeProgress();
                Log.e("test", "onFailure" + errorResponse);
                Toast.makeText(context, "请检查网络。。", Toast.LENGTH_LONG).show();
            }

        });
    }

    //注册验证码
    public void authCode(final String type, final String mobile, final Handler handler, final Context mContext) {
        RequestParams params = new RequestParams();
        params.put(HttpKey.TYPE, type);
        params.put(HttpKey.MOBIL, mobile);
        HttpClient.getInstance().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        HttpClient.getInstance().post(HttpUrl.authCodedUrl, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.e("test", "onSuccess-----" + response);
                Message msg = Message.obtain();
                msg.what = HttpCode.AUTHCODE_SUCESS;
                msg.obj = response;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                Toast.makeText(mContext, "请检查网络。。" + errorResponse, Toast.LENGTH_LONG).show();
            }
        });
    }


    //手机号是否注册
    public void isregistered(final String mobile, final Handler handler, final Context mContext) {
        RequestParams params = new RequestParams();
        params.put(HttpKey.USERNAME, mobile);
        HttpClient.getInstance().get(HttpUrl.isRegiterUrl + "?mobile=" + mobile, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.e("test", "isregistered" + response.toString());
                Message msg = Message.obtain();
                msg.what = HttpCode.IS_REFISTER;
                msg.obj = response;
                handler.sendMessage(msg);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                Toast.makeText(mContext, "失敗" + errorResponse, Toast.LENGTH_LONG).show();

            }
        });
    }

    //修改密码
    public void changepwd(final Context context, String username, String old_pwd1, String newpassword3) {
        RequestParams params = new RequestParams();
        params.put(HttpKey.USERNAME, username);
        params.put(HttpKey.OLDPASS, old_pwd1);
        params.put(HttpKey.NEWPASS, newpassword3);
        HttpClient.getInstance().addHeader("Accept", "*/*");
        HttpClient.getInstance().setTimeout(10 * 1000);
        HttpClient.getInstance().post(HttpUrl.changepassword, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    String status = jsonObject.optString("status");
                    if (status.equals("100")) {
                        Log.e("test", "onSuccess" + new String(bytes));
                        Toast.makeText(context, "密码修改成功!", Toast.LENGTH_LONG).show();
                        SaveParam.getInstance().clearData((Activity) context);
                        Intent intent = new Intent();
                        intent.setClass(context, LoginAndRegisterActivity.class);
                        ((Activity) context).startActivity(intent);
                        ((Activity) context).finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.e("test", "onFailure" + new String(bytes));
                Toast.makeText(context, "密码修改失败,请检查网络!", Toast.LENGTH_LONG).show();
                onFinish();
            }
        });

    }

    //忘记密码
    public void forgetpassword(final Context context, String forot_pwd_phone1, String forot_pwd_pwd3, String forot_pwd_anthcode1, Handler handler) {
        RequestParams params = new RequestParams();
        params.put(HttpKey.USERNAME, forot_pwd_phone1);
        params.put(HttpKey.PASSWORD, forot_pwd_pwd3);
        params.put(HttpKey.AUTHCODE, forot_pwd_anthcode1);
        HttpClient.getInstance().addHeader("Accept", "*/*");
        HttpClient.getInstance().post(HttpUrl.forgetpassword, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    String status = jsonObject.optString("status");
                    if (status.equals("100")) {
                        Log.e("test", "onSuccess" + new String(bytes));
                        Toast.makeText(context, "密码修改成功!", Toast.LENGTH_LONG).show();
                        ((Activity) context).finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.e("test", "onFailure" + new String(bytes));
                Toast.makeText(context, "忘记密码修改失败,请检查网络!", Toast.LENGTH_LONG).show();
            }
        });

    }

    //保存用户登录信息
    public void saveLoginInfo(Context context, String username, String password, String access_token, String loginStatus, String userId) {
        SaveParam.getInstance().saveLoginParam(context, "username", username);
        SaveParam.getInstance().saveLoginParam(context, "password", password);
        SaveParam.getInstance().saveLoginParam(context, "access_token", access_token);
        SaveParam.getInstance().saveLoginParam(context, "login", loginStatus);
        SaveParam.getInstance().saveLoginParam(context, "userId", userId);


    }

    //保存用户个人信息
    public void savePresonInfo(Context context, String lastname, String gender, String mobilePortrait) {
        SaveParam.getInstance().saveLoginParam(context, "lastname", lastname);
        SaveParam.getInstance().saveLoginParam(context, "gender", gender);
        SaveParam.getInstance().saveLoginParam(context, "mobilePortrait", mobilePortrait);
    }

    //提交保卡信息
    public void subcardinfo(final Context context, int userId, String customerName, String customerAddress,
                            String buyAddress, String buyTime, String alterSaleCall,
                            String productModel, String deviceNumber, String macAddress,
                            String mobilePhone) {
        mProgress = DialogPermission.showProgress(context, null, "正在绑定保卡...",
                false, true, null);
        RequestParams params = new RequestParams();

        params.put(HttpKey.USEID, userId);
        params.put(HttpKey.CUSTOMENAME, customerName);
        params.put(HttpKey.CUSTOMADRESS, customerAddress);
        params.put(HttpKey.BUYADREES, buyAddress);
        params.put(HttpKey.BUYTIME, buyTime);
        params.put(HttpKey.ALTERSALECALL, alterSaleCall);
        params.put(HttpKey.PRODUCTMODEL, productModel);
        params.put(HttpKey.DEVICENUMBER, deviceNumber);
        params.put(HttpKey.MACADRESS, macAddress);
        params.put(HttpKey.MOBILPHONE, mobilePhone);

        HttpClient.getInstance().addHeader("Accept", "*/*");

        Log.e("test", "params" + params);
        HttpClient.getInstance().setTimeout(5 * 1000);
        HttpClient.getInstance().post(context, HttpUrl.subcardinfo, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                JSONObject jsonObject = null;
                closeProgress();
                try {
                    jsonObject = new JSONObject(new String(bytes));
                    String status = jsonObject.optString("status");
                    if (status.equals("1")) {
                        Log.e("test", "onSuccess" + jsonObject);
                        Toast.makeText(context, "保卡绑定成功!", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent();
                        intent.setClass((Activity) context, TheStartPageActivity.class);
                        ((Activity) context).startActivity(intent);
                        ((Activity) context).overridePendingTransition(R.anim.rightin, R.anim.rightout);
                        ((Activity) context).finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.e("test", "onFailure" + (throwable));
                closeProgress();
                Toast.makeText(context, "保卡绑定失败!,请检查网络", Toast.LENGTH_LONG).show();

            }
        });

    }
    //获取保卡信息
    public void getcardinfo(final Context mContext, int userId , final Handler handler) {
        mProgress = DialogPermission.showProgress(mContext, null, "正在获取保卡信息...",
                false, true, null);
        HttpClient.getInstance().setTimeout(5 * 1000);
        HttpClient.getInstance().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        HttpClient.getInstance().get(HttpUrl.getcardinfo+ "?userId=" + userId , new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                closeProgress();
                Log.e("test", "省" + new String(arg2));
                Message msg = Message.obtain();
                msg.what = HttpCode.SUCHCARDINFO;
                msg.obj = new String(arg2);
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                closeProgress();
                Toast.makeText(mContext, "请检查网络。。" , Toast.LENGTH_LONG).show();
            }
        });
    }
    //修改保卡信息电话
    public void changecardinfophone(final Context mContext, int userId, final String customerPhone, String authCode) {
        RequestParams params = new RequestParams();
        params.put("userId", userId);
        params.put("customerPhone", customerPhone);
        params.put("authCode", authCode);
        HttpClient.getInstance().addHeader("Accept", "*/*");
        HttpClient.getInstance().post(HttpUrl.changecardinfo, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    String status = jsonObject.optString("status");
                    if (status.equals("1")) {
                        Toast.makeText(mContext, "手机号修改成功", Toast.LENGTH_LONG).show();
                        SaveParam.getInstance().saveCustomizeParam(mContext,SaveParam.CARDPHONE,  customerPhone);
                        ((Activity) mContext).finish();
                    }else if (status.equals("1001")){
                        Toast.makeText(mContext, "验证码输入错误", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.e("test", "onFailure" + new String(bytes));
                Toast.makeText(mContext, "请检查网络。。" + new String(bytes), Toast.LENGTH_LONG).show();
            }
        });
    }
    //修改保卡信息地址
    public void changecardinfo(final Context mContext,int userId,String customerPhone,String authCode,String customerAddress) {
        RequestParams params = new RequestParams();
        params.put("userId", userId);
        params.put("customerPhone", customerPhone);
        params.put("authCode", authCode);
        params.put("customerAddress", customerAddress);
        HttpClient.getInstance().addHeader("Accept", "*/*");
        HttpClient.getInstance().post(HttpUrl.changecardinfo, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    String status = jsonObject.optString("status");
                    if (status.equals("1")) {
                        Log.e("test", "省" + jsonObject);
                        Toast.makeText(mContext, "成功。。" + jsonObject, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.e("test", "onFailure" + new String(bytes));
                Toast.makeText(mContext, "请检查网络。。" + new String(bytes), Toast.LENGTH_LONG).show();
            }
        });
    }
    //验证是否保卡信息
    public void cardinfocheck(final Context mContext, int userId, final Handler handler) {
        HttpClient.getInstance().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        HttpClient.getInstance().get(HttpUrl.cardcheck+ "?userId=" + userId, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                Log.e("test", "省" + new String(arg2));
                Message msg = Message.obtain();
                msg.what = HttpCode.CHECKCARD;
                msg.obj = new String(arg2);
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                Toast.makeText(mContext, "请检查网络。。" + arg3, Toast.LENGTH_LONG).show();
            }
        });
    }




    ///省级接口
    public void provices(final Context mContext) {
        HttpClient.getInstance().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        HttpClient.getInstance().get(HttpUrl.provinceUrl, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                Log.e("test", "省" + new String(arg2));
                ProvinceInfo provinceInfo = GsonTool.getPerson(new String(arg2), ProvinceInfo.class);//解析json数据
                Log.e("test", "状态码" + provinceInfo.getStatus());
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < provinceInfo.getProvinces().size(); i++) {
                    sb.append(provinceInfo.getProvinces().get(i).getRegionName() + ",");

                }
                Log.e("test", "省" + sb.toString());
                SaveParam.getInstance().saveLoginParam(mContext, SaveParam.PROVINCES, "" + sb.toString());


            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                Toast.makeText(mContext, "请检查网络。。" + arg3, Toast.LENGTH_LONG).show();
            }
        });
    }

    //市级接口
    public void cityinfo(final Context mContext, int regionId) {
        HttpClient.getInstance().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        HttpClient.getInstance().get(HttpUrl.cityUrl + "?regionId=" + regionId, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                Log.e("test", "市" + new String(arg2));
                Gson gson = new Gson();
                CityInfo cityInfo = gson.fromJson(new String(arg2), CityInfo.class);
                for (int i = 0; i < cityInfo.getCities().size(); i++) {
                    Log.e("test", "市" + cityInfo.getCities().get(i)
                            .getParentId());
                    Log.e("test", "市" + cityInfo.getCities().get(i)
                            .getRegionId());
                    Log.e("test", "市" + cityInfo.getCities().get(i).getRegionName() + "");

                }


            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                Toast.makeText(mContext, "请检查网络。。" + arg3, Toast.LENGTH_LONG).show();
            }
        });
    }

    //区县级接口
    public void countyinfo(final Context mContext, int parentId) {
        HttpClient.getInstance().addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        HttpClient.getInstance().get(HttpUrl.countyUrl + "?regionId=" + parentId, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                Log.e("test", "区" + new String(arg2));
                Gson gson = new Gson();
                CountyInfo countyInfo = gson.fromJson(new String(arg2), CountyInfo.class);
                for (int i = 0; i < countyInfo.getCounties().size(); i++) {
                    Log.e("test", "区" + countyInfo.getCounties().get(i)
                            .getParentId());
                    Log.e("test", "区" + countyInfo.getCounties().get(i)
                            .getRegionId());
                    Log.e("test", "区" + countyInfo.getCounties().get(i).getRegionName() + "");

                }


            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                Toast.makeText(mContext, "请检查网络。。" + arg3, Toast.LENGTH_LONG).show();
            }
        });
    }


    private void closeProgress() {
        try {
            if (mProgress != null) {
                mProgress.dismiss();
                mProgress = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}