d
This commit is contained in:
@@ -147,6 +147,8 @@ android {
|
||||
implementation 'com.j256.ormlite:ormlite-android:5.1'
|
||||
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
|
||||
|
||||
|
||||
implementation 'com.lzy.net:okgo:3.0.4'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.RequestOptions;
|
||||
import com.cutil.ScreenUtils;
|
||||
@@ -31,6 +32,12 @@ import com.gyf.immersionbar.ImmersionBar;
|
||||
import com.lxj.xpopup.XPopup;
|
||||
import com.lzy.ninegrid.ImageInfo;
|
||||
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
|
||||
import com.lzy.okgo.OkGo;
|
||||
import com.lzy.okgo.callback.StringCallback;
|
||||
import com.lzy.okgo.model.HttpHeaders;
|
||||
import com.lzy.okgo.model.HttpParams;
|
||||
import com.lzy.okgo.model.Response;
|
||||
import com.lzy.okgo.request.base.Request;
|
||||
import com.scwang.smartrefresh.layout.api.RefreshLayout;
|
||||
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
|
||||
import com.selectpicker.OptionsPopupWindow;
|
||||
@@ -42,8 +49,8 @@ import com.sl.house_property.db.dao.CommentDao;
|
||||
import com.sl.house_property.discovery.GoodsInfoDialog;
|
||||
import com.sl.house_property.discovery.HistoryRecordActivity;
|
||||
import com.sl.house_property.discovery.HistoryRecordActivitytwo;
|
||||
import com.sl.house_property.discovery.PropertyActivity;
|
||||
import com.sl.house_property.discovery.ShareCodeActivity;
|
||||
import com.sl.house_property.okgonet.HttpConstants;
|
||||
import com.sl.house_property.okgonet.Observer;
|
||||
import com.sl.house_property.order.SettleActivity;
|
||||
import com.sl.house_property.user.MyMarketShopActivity;
|
||||
import com.sl.house_property.user.MyPropertyActivity;
|
||||
@@ -70,6 +77,7 @@ import entity.DiscoveryListEntity;
|
||||
import entity.GoodsDetailEntity;
|
||||
import entity.HomelistitemEntity;
|
||||
import entity.MainF1Entity;
|
||||
import entity.MyAddress;
|
||||
import entity.RegisterUser;
|
||||
import http.ApiConfig;
|
||||
import my_loader.Loader;
|
||||
@@ -82,6 +90,7 @@ import tools.Config;
|
||||
import utils.CommonUtils;
|
||||
import utils.DateUtils;
|
||||
import utils.DateUtilss;
|
||||
import utils.JsonUtils;
|
||||
import utils.KeyboardUtil;
|
||||
import utils.Md5;
|
||||
import utils.SPUtils;
|
||||
@@ -120,6 +129,8 @@ public class MainFragment extends BaseFragment<FragmentMainBinding> implements V
|
||||
private RegisterUser user;
|
||||
private String miyuetime;
|
||||
private String wy_id="";
|
||||
private String areatwo="";
|
||||
private String userid="";
|
||||
|
||||
public MainFragment() {
|
||||
// Required empty public constructor
|
||||
@@ -169,10 +180,7 @@ public class MainFragment extends BaseFragment<FragmentMainBinding> implements V
|
||||
;
|
||||
}
|
||||
// String wy_id=(String) SPUtils.getSp(getActivity(), DoConfig.MY_HOUST_WYID,"");
|
||||
String area=(String) SPUtils.getSp(getActivity(), DoConfig.MY_HOUST_ADDRESS,"");
|
||||
if(!area.equals("")){
|
||||
wy_id="-";
|
||||
}
|
||||
|
||||
if (wy_id.equals("")) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
RegisterUser registerUser = Config.getInstance(getContext()).getUser();
|
||||
@@ -188,6 +196,7 @@ public class MainFragment extends BaseFragment<FragmentMainBinding> implements V
|
||||
getGankList(ApiConfig.BASE_URL, map, "", 1);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private ArrayList<HomelistitemEntity> list;
|
||||
@@ -213,6 +222,11 @@ public class MainFragment extends BaseFragment<FragmentMainBinding> implements V
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
areatwo=(String) SPUtils.getSp(getActivity(), DoConfig.MY_HOUST_ADDRESS,"");
|
||||
if(!areatwo.equals("")){
|
||||
wy_id="-";
|
||||
requestVersion(areatwo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -245,6 +259,7 @@ public class MainFragment extends BaseFragment<FragmentMainBinding> implements V
|
||||
switch (getcode1) {
|
||||
|
||||
case 0:
|
||||
Log.e("测试1","");
|
||||
Gson gs = new Gson();
|
||||
LinkedTreeMap adta = (LinkedTreeMap) resultcode.data;
|
||||
String s = gs.toJson(adta);
|
||||
@@ -286,6 +301,7 @@ public class MainFragment extends BaseFragment<FragmentMainBinding> implements V
|
||||
|
||||
break;
|
||||
case 1:
|
||||
Log.e("测试2","");
|
||||
Gson gss = new Gson();
|
||||
LinkedTreeMap adtaa = (LinkedTreeMap) resultcode.data;
|
||||
String ss = gss.toJson(adtaa);
|
||||
@@ -1424,4 +1440,67 @@ public class MainFragment extends BaseFragment<FragmentMainBinding> implements V
|
||||
});
|
||||
baseActivity.addSubscription(subscribe);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void requestVersion(String area) {
|
||||
|
||||
RegisterUser registerUser = Config.getInstance(getContext()).getUser();
|
||||
if (registerUser != null) {
|
||||
userid=registerUser.getUserid();
|
||||
}
|
||||
OkGo.<String>post(HttpConstants.URiBase)
|
||||
.params("app", "Cas")
|
||||
.params("sign", Md5.md5("Cas" + "GetMyAddress" + Md5.secret))
|
||||
.params("userid", userid)
|
||||
.params("class", "GetMyAddress")
|
||||
.execute(new StringCallback() {
|
||||
@Override
|
||||
public void onStart(Request<String, ? extends Request> request) {
|
||||
super.onStart(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(Response<String> response) {
|
||||
|
||||
Log.e("onSuccess", response.body());
|
||||
String body = (String) response.body();
|
||||
|
||||
MyAddress groupStatisticalBean = JsonUtils.fromJson(body, MyAddress.class);
|
||||
|
||||
if(groupStatisticalBean.getStatus()==0){
|
||||
list = (ArrayList<HomelistitemEntity>) groupStatisticalBean.getData().getHome();
|
||||
final ArrayList<String> strings = new ArrayList<>();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
strings.add(list.get(i).getAddress());
|
||||
}
|
||||
if (list.size() > 0) {
|
||||
for(int i=0;i<list.size();i++){
|
||||
HomelistitemEntity homelistitemEntity = list.get(i);
|
||||
if(homelistitemEntity.getArea().equals(areatwo)){
|
||||
wy_id = homelistitemEntity.getWy_id();
|
||||
Log.e("wy_id",wy_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void onError(Response<String> response) {
|
||||
super.onError(response);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
super.onFinish();
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,30 @@ import android.support.multidex.MultiDex;
|
||||
import android.util.Log;
|
||||
|
||||
import com.lzy.ninegrid.NineGridView;
|
||||
import com.lzy.okgo.OkGo;
|
||||
import com.lzy.okgo.cache.CacheEntity;
|
||||
import com.lzy.okgo.cache.CacheMode;
|
||||
import com.lzy.okgo.cookie.CookieJarImpl;
|
||||
import com.lzy.okgo.cookie.store.DBCookieStore;
|
||||
import com.lzy.okgo.https.HttpsUtils;
|
||||
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
|
||||
import com.lzy.okgo.model.HttpHeaders;
|
||||
import com.lzy.okgo.model.HttpParams;
|
||||
import com.sl.house_property.db.dao.DataBaseHelper;
|
||||
import com.tencent.bugly.Bugly;
|
||||
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLSession;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import tools.CrashHandler;
|
||||
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
|
||||
import utils.NineImageLoader;
|
||||
@@ -36,6 +54,7 @@ public class MyApplication extends Application{
|
||||
*/
|
||||
public static Context getContext() {
|
||||
return context;
|
||||
|
||||
}
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
@@ -64,6 +83,7 @@ public class MyApplication extends Application{
|
||||
Log.d(TAG, "[ExampleApplication] onCreate");
|
||||
super.onCreate();
|
||||
instance = this;
|
||||
initOkGo();
|
||||
//
|
||||
// CrashHandler crashHandler = CrashHandler.getInstance();
|
||||
// crashHandler.init(this);
|
||||
@@ -95,4 +115,110 @@ public class MyApplication extends Application{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void initOkGo() {
|
||||
//---------这里给出的是示例代码,告诉你可以这么传,实际使用的时候,根据需要传,不需要就不传-------------//
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.put("commonHeaderKey1", "commonHeaderValue1"); //header不支持中文,不允许有特殊字符
|
||||
headers.put("commonHeaderKey2", "commonHeaderValue2");
|
||||
HttpParams params = new HttpParams();
|
||||
params.put("commonParamsKey1", "commonParamsValue1"); //param支持中文,直接传,不要自己编码
|
||||
params.put("commonParamsKey2", "这里支持中文参数");
|
||||
//----------------------------------------------------------------------------------------//
|
||||
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
//log相关
|
||||
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
|
||||
loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY); //log打印级别,决定了log显示的详细程度
|
||||
loggingInterceptor.setColorLevel(Level.INFO); //log颜色级别,决定了log在控制台显示的颜色
|
||||
builder.addInterceptor(loggingInterceptor); //添加OkGo默认debug日志
|
||||
//第三方的开源库,使用通知显示当前请求的log,不过在做文件下载的时候,这个库好像有问题,对文件判断不准确
|
||||
//builder.addInterceptor(new ChuckInterceptor(this));
|
||||
|
||||
//超时时间设置,默认60秒
|
||||
builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); //全局的读取超时时间
|
||||
builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); //全局的写入超时时间
|
||||
builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); //全局的连接超时时间
|
||||
|
||||
//自动管理cookie(或者叫session的保持),以下几种任选其一就行
|
||||
//builder.cookieJar(new CookieJarImpl(new SPCookieStore(this))); //使用sp保持cookie,如果cookie不过期,则一直有效
|
||||
builder.cookieJar(new CookieJarImpl(new DBCookieStore(this))); //使用数据库保持cookie,如果cookie不过期,则一直有效
|
||||
//builder.cookieJar(new CookieJarImpl(new MemoryCookieStore())); //使用内存保持cookie,app退出后,cookie消失
|
||||
|
||||
//https相关设置,以下几种方案根据需要自己设置
|
||||
//方法一:信任所有证书,不安全有风险
|
||||
HttpsUtils.SSLParams sslParams1 = HttpsUtils.getSslSocketFactory();
|
||||
//方法二:自定义信任规则,校验服务端证书
|
||||
HttpsUtils.SSLParams sslParams2 = HttpsUtils.getSslSocketFactory(new SafeTrustManager());
|
||||
//方法三:使用预埋证书,校验服务端证书(自签名证书)
|
||||
//HttpsUtils.SSLParams sslParams3 = HttpsUtils.getSslSocketFactory(getAssets().open("srca.cer"));
|
||||
//方法四:使用bks证书和密码管理客户端证书(双向认证),使用预埋证书,校验服务端证书(自签名证书)
|
||||
//HttpsUtils.SSLParams sslParams4 = HttpsUtils.getSslSocketFactory(getAssets().open("xxx.bks"), "123456", getAssets().open("yyy.cer"));
|
||||
builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
|
||||
//配置https的域名匹配规则,详细看demo的初始化介绍,不需要就不要加入,使用不当会导致https握手失败
|
||||
builder.hostnameVerifier(new SafeHostnameVerifier());
|
||||
|
||||
// 其他统一的配置
|
||||
// 详细说明看GitHub文档:https://github.com/jeasonlzy/
|
||||
OkGo.getInstance().init(this) //必须调用初始化
|
||||
.setOkHttpClient(builder.build()) //建议设置OkHttpClient,不设置会使用默认的
|
||||
.setCacheMode(CacheMode.NO_CACHE) //全局统一缓存模式,默认不使用缓存,可以不传
|
||||
.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE) //全局统一缓存时间,默认永不过期,可以不传
|
||||
.setRetryCount(3); //全局统一超时重连次数,默认为三次,那么最差的情况会请求4次(一次原始请求,三次重连请求),不需要可以设置为0
|
||||
// .addCommonHeaders(headers) //全局公共头
|
||||
// .addCommonParams(params); //全局公共参数
|
||||
}
|
||||
|
||||
/**
|
||||
* 这里只是我谁便写的认证规则,具体每个业务是否需要验证,以及验证规则是什么,请与服务端或者leader确定
|
||||
* 这里只是我谁便写的认证规则,具体每个业务是否需要验证,以及验证规则是什么,请与服务端或者leader确定
|
||||
* 这里只是我谁便写的认证规则,具体每个业务是否需要验证,以及验证规则是什么,请与服务端或者leader确定
|
||||
* 重要的事情说三遍,以下代码不要直接使用
|
||||
*/
|
||||
private class SafeTrustManager implements X509TrustManager {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
try {
|
||||
for (X509Certificate certificate : chain) {
|
||||
certificate.checkValidity(); //检查证书是否过期,签名是否通过等
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new CertificateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 这里只是我谁便写的认证规则,具体每个业务是否需要验证,以及验证规则是什么,请与服务端或者leader确定
|
||||
* 这里只是我谁便写的认证规则,具体每个业务是否需要验证,以及验证规则是什么,请与服务端或者leader确定
|
||||
* 这里只是我谁便写的认证规则,具体每个业务是否需要验证,以及验证规则是什么,请与服务端或者leader确定
|
||||
* 重要的事情说三遍,以下代码不要直接使用
|
||||
*/
|
||||
private class SafeHostnameVerifier implements HostnameVerifier {
|
||||
@Override
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
//验证主机名是否匹配
|
||||
//return hostname.equals("server.jeasonlzy.com");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
15
app/src/main/java/com/sl/house_property/okgonet/Func1.java
Normal file
15
app/src/main/java/com/sl/house_property/okgonet/Func1.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.sl.house_property.okgonet;
|
||||
|
||||
/**
|
||||
* ================================================
|
||||
* 作 者:jeasonlzy(廖子尧)Github地址:https://github.com/jeasonlzy
|
||||
* 版 本:1.0
|
||||
* 创建日期:2017/2/6
|
||||
* 描 述:
|
||||
* 修订历史:
|
||||
* ================================================
|
||||
*/
|
||||
public interface Func1<T, R> {
|
||||
//把一个结果转为我们需要的一个结果
|
||||
R call(T t);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.sl.house_property.okgonet;
|
||||
|
||||
/**
|
||||
* 项目名:JiaJieSong
|
||||
* 包名:com.fanghoo.jiajiesong.http
|
||||
* 文件名:HttpConstants
|
||||
* 创建者:任剑波
|
||||
* 创建时间:2017/10/22 0022 23:05
|
||||
* 描述:所有请求相关地址
|
||||
*/
|
||||
public class HttpConstants {
|
||||
|
||||
public static String URiBase = "http://wy.dou1.net/api/";
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.sl.house_property.okgonet;
|
||||
|
||||
|
||||
//定义一个动作
|
||||
public abstract class Observable<T> {
|
||||
public abstract void subscribe(Observer<T> observer);
|
||||
|
||||
public <R> Observable<R> map(final Func1<T,R> func1){
|
||||
final Observable<T> source=this;
|
||||
return new Observable<R>() {
|
||||
@Override
|
||||
public void subscribe(final Observer<R> observer) {
|
||||
source.subscribe(new Observer<T>() {
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
R r=func1.call(t);
|
||||
observer.onNext(r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
observer.onError(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public <R> Observable<R> flatMap(final Func1<T, Observable<R>> func1) {
|
||||
final Observable<T> source=this;
|
||||
return new Observable<R>() {
|
||||
@Override
|
||||
public void subscribe(final Observer<R> observer) {
|
||||
source.subscribe(new Observer<T>() {
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
final Observable<R> call = func1.call(t);
|
||||
call.subscribe(new Observer<R>() {
|
||||
@Override
|
||||
public void onNext(R r) {
|
||||
observer.onNext(r);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
observer.onError(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.sl.house_property.okgonet;
|
||||
|
||||
public interface Observer<T> {
|
||||
void onNext(T t);
|
||||
void onError(Exception e);
|
||||
}
|
||||
49
app/src/main/java/entity/MyAddress.java
Normal file
49
app/src/main/java/entity/MyAddress.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public class MyAddress implements Serializable {
|
||||
|
||||
private int status;
|
||||
private String msg;
|
||||
private DataDTO data;
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public DataDTO getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(DataDTO data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static class DataDTO {
|
||||
private List<HomelistitemEntity> home;
|
||||
|
||||
public List<HomelistitemEntity> getHome() {
|
||||
return home;
|
||||
}
|
||||
|
||||
public void setHome(List<HomelistitemEntity> home) {
|
||||
this.home = home;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
126
app/src/main/java/utils/JsonUtils.java
Normal file
126
app/src/main/java/utils/JsonUtils.java
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) 2016 android@19code.com
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Json工具类
|
||||
*
|
||||
* Create by h4de5ing 2016/5/7 007
|
||||
*/
|
||||
public class JsonUtils {
|
||||
public static String toJson(Object obj) {
|
||||
Gson gson = new Gson();
|
||||
return gson.toJson(obj);
|
||||
}
|
||||
|
||||
public static <T> T fromJson(String str, Class<T> type) {
|
||||
Gson gson = new Gson();
|
||||
return gson.fromJson(str, type);
|
||||
}
|
||||
|
||||
public static JSONObject map2Json(Map<?, ?> data) {
|
||||
JSONObject object = new JSONObject();
|
||||
|
||||
for (Map.Entry<?, ?> entry : data.entrySet()) {
|
||||
String key = (String) entry.getKey();
|
||||
if (key == null) {
|
||||
throw new NullPointerException("key == null");
|
||||
}
|
||||
try {
|
||||
object.put(key, wrap(entry.getValue()));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
public static JSONArray collection2Json(Collection<?> data) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
if (data != null) {
|
||||
for (Object aData : data) {
|
||||
jsonArray.put(wrap(aData));
|
||||
}
|
||||
}
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
public static JSONArray object2Json(Object data) throws JSONException {
|
||||
if (!data.getClass().isArray()) {
|
||||
throw new JSONException("Not a primitive data: " + data.getClass());
|
||||
}
|
||||
final int length = Array.getLength(data);
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (int i = 0; i < length; ++i) {
|
||||
jsonArray.put(wrap(Array.get(data, i)));
|
||||
}
|
||||
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
private static Object wrap(Object o) {
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
if (o instanceof JSONArray || o instanceof JSONObject) {
|
||||
return o;
|
||||
}
|
||||
try {
|
||||
if (o instanceof Collection) {
|
||||
return collection2Json((Collection<?>) o);
|
||||
} else if (o.getClass().isArray()) {
|
||||
return object2Json(o);
|
||||
}
|
||||
if (o instanceof Map) {
|
||||
return map2Json((Map<?, ?>) o);
|
||||
}
|
||||
|
||||
if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double || o instanceof Float || o instanceof Integer || o instanceof Long
|
||||
|| o instanceof Short || o instanceof String) {
|
||||
return o;
|
||||
}
|
||||
if (o.getClass().getPackage().getName().startsWith("java.")) {
|
||||
return o.toString();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static JSONObject string2JSONObject(String json) {
|
||||
JSONObject jsonObject = null;
|
||||
try {
|
||||
JSONTokener jsonParser = new JSONTokener(json);
|
||||
jsonObject = (JSONObject) jsonParser.nextValue();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user