|
|
@@ -8,8 +8,10 @@ import android.util.Log;
|
|
|
|
|
|
import com.afollestad.materialdialogs.MaterialDialog;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.xzjmyk.pm.activity.MyApplication;
|
|
|
import com.xzjmyk.pm.activity.ui.erp.net.HttpUtil;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -18,6 +20,17 @@ import java.util.Map;
|
|
|
*/
|
|
|
public class ViewUtil {
|
|
|
|
|
|
+ //TODO 待采用更好的方式处理 定义的属性太多了
|
|
|
+ private static boolean isNetByHandler = false;//判断是否是经过重登录后的请求
|
|
|
+ private static String url;
|
|
|
+ private static Context ct;
|
|
|
+ private static Handler h;
|
|
|
+ private static Map<String, Object> params;
|
|
|
+ private static LinkedHashMap<String, Object> headers;
|
|
|
+ private static int what;
|
|
|
+ private static Message message;
|
|
|
+ private static Bundle bundle;
|
|
|
+ private static String request;
|
|
|
public static HttpUtil.Response httpSendTask(
|
|
|
String url,
|
|
|
Map<String, Object> params,
|
|
|
@@ -26,10 +39,10 @@ public class ViewUtil {
|
|
|
HttpUtil.Response response = null;
|
|
|
try {
|
|
|
if (method.equals("get")) {
|
|
|
- response = HttpUtil.sendGetRequest(url, params, headers,false);
|
|
|
+ response = HttpUtil.sendGetRequest(url, params, headers, false);
|
|
|
}
|
|
|
if (method.equals("post")) {
|
|
|
- response = HttpUtil.sendGetRequest(url, params, headers,false);
|
|
|
+ response = HttpUtil.sendGetRequest(url, params, headers, false);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -37,10 +50,46 @@ public class ViewUtil {
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
+ private Handler handler = new Handler() {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ if (msg.what == 500) {//重登陆
|
|
|
+ LoginERPTask();
|
|
|
+ } else if (msg.what == 0x11) {
|
|
|
+ //判断登陆成功
|
|
|
+ if (msg.getData() != null && com.xzjmyk.pm.activity.util.StringUtils.isEmpty(msg.getData().getString("result"))) {
|
|
|
+ JsonValidator jva = new JsonValidator();
|
|
|
+ String json = msg.getData().getString("result");
|
|
|
+ if (jva.validate(json)) {
|
|
|
+ if (JSON.parseObject(json).containsKey("success") && JSON.parseObject(json).getBoolean("success")) {
|
|
|
+ isNetByHandler = true;
|
|
|
+ httpSendRequest(ct, url, params, handler, headers, what, message, bundle, request);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 登录ERP gongpengming
|
|
|
+ public void LoginERPTask() {
|
|
|
+ Context ct = MyApplication.getInstance();
|
|
|
+ String url = Constants.getAppBaseUrl(ct) + "mobile/login.action";
|
|
|
+ String master = CommonUtil.getSharedPreferences(ct, "erp_master");
|
|
|
+ String phone = CommonUtil.getSharedPreferences(ct, "user_phone");
|
|
|
+ String password = CommonUtil.getSharedPreferences(ct, "user_password");
|
|
|
+ Map<String, String> params = new HashMap<String, String>();
|
|
|
+ params.put("username", phone);
|
|
|
+ params.put("password", password);
|
|
|
+ params.put("master", master);
|
|
|
+ com.xzjmyk.pm.activity.ui.erp.net.ViewUtil.startNetThread(url, params, handler, 0x11, null, null, "post");
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @author Administrator
|
|
|
* @功能:封装网络请求 httpclient4.3
|
|
|
*/
|
|
|
+ @Deprecated
|
|
|
public static void httpSendRequest(
|
|
|
final Context ct,
|
|
|
final String url,
|
|
|
@@ -51,15 +100,14 @@ public class ViewUtil {
|
|
|
final Message message,
|
|
|
final Bundle bundle,
|
|
|
final String request) {
|
|
|
- new Thread(new Runnable() {
|
|
|
+ new Thread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
boolean isNetHas = Constants.isNetworkConnected(ct);
|
|
|
if (isNetHas) {
|
|
|
- Log.i("result:","url="+url);
|
|
|
+ Log.i("result:", "url=" + url);
|
|
|
HttpUtil.Response result = httpSendTask(url, params, headers, request);
|
|
|
if (result != null) {
|
|
|
-
|
|
|
prinltResponseInfo(result, url, params);
|
|
|
if (result.getStatusCode() == 200) {
|
|
|
if (bundle == null || message == null) {
|
|
|
@@ -83,11 +131,12 @@ public class ViewUtil {
|
|
|
} else {
|
|
|
exception = "500系统错误";
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
if (result.getStatusCode() == 404) {
|
|
|
exception = "404系统错误";
|
|
|
}
|
|
|
- Log.i("Http", "非200响应:" + result.getResponseText());
|
|
|
+
|
|
|
Bundle bundle = new Bundle();
|
|
|
Message message = new Message();
|
|
|
bundle.putString("result", exception);
|
|
|
@@ -118,13 +167,12 @@ public class ViewUtil {
|
|
|
}
|
|
|
|
|
|
private static void prinltResponseInfo(HttpUtil.Response result, String url, Map<String, Object> params) {
|
|
|
-
|
|
|
Log.i("result", "http 请求-----------------------------------------");
|
|
|
Log.i("result", "result:" + result);
|
|
|
Log.i("result", "url:" + url);
|
|
|
Log.i("result", "parm:" + params.toString());
|
|
|
Log.i("result", "statusCode:" + result.getStatusCode());
|
|
|
- LogUtil.prinlnLongMsg("result",result.getResponseText());
|
|
|
+ LogUtil.prinlnLongMsg("result", result.getResponseText());
|
|
|
Log.i("result", "http 响应-----------------------------------------");
|
|
|
}
|
|
|
|
|
|
@@ -147,6 +195,4 @@ public class ViewUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
}
|