一、获取Json串
String jsonString = PhoneUtil.doRequestForString(mContext, url, PhoneUtil.HTTP_POST, params, true);
public static String doRequestForString(Context context, String serverUrl, int remoteType, Map<String, String> hashMap, boolean log) throws Exception { int timeoutConnection = mTimeoutConnection; int timeoutSocket = mTimeoutSocket; return doRequestForString(context, serverUrl, remoteType, hashMap, timeoutConnection, timeoutSocket, log); } public static String doRequestForString(Context context, String serverUrl, int remoteType, Map<String, String> hashMap, int timeoutConnection, int timeoutSocket, boolean log) throws Exception { String result = ""; HttpEntity entity = doRequestForEntity(context, serverUrl, remoteType, hashMap, timeoutConnection, timeoutSocket, log); if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); if (log) Log.i("=====", serverUrl + " 请求结果:" + result); } else { if (log) Log.i("=====", serverUrl + " 请求结果为空"); } return result; } public static HttpEntity doRequestForEntity(Context context, String serverUrl, int remoteType, Map<String, String> hashMap, boolean log) throws Exception { int timeoutConnection = mTimeoutConnection; int timeoutSocket = mTimeoutSocket; return doRequestForEntity(context, serverUrl, remoteType, hashMap, timeoutConnection, timeoutSocket, log); } public static HttpEntity doRequestForEntity(Context context, String serverUrl, int remoteType, Map<String, String> hashMap, int timeoutConnection, int timeoutSocket, boolean log) throws Exception { StringBuilder sb = new StringBuilder(serverUrl); if (hashMap != null) { for (Entry<String, String> entry : hashMap.entrySet()) { if (!sb.toString().contains("?")) sb.append("?" + entry.getKey() + "=" + entry.getValue()); else sb.append("&" + entry.getKey() + "=" + entry.getValue()); } } if (log) Log.d("====", sb + " 发起网络请求"); HttpEntity entity = null; HttpUriRequest request = null; if (remoteType == HTTP_POST) { if (hashMap != null) { List<NameValuePair> params = new ArrayList<NameValuePair>(); for (Entry<String, String> entry : hashMap.entrySet()) { params.add(new BasicNameValuePair(entry.getKey(), entry .getValue())); } HttpPost post = new HttpPost(serverUrl); post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); request = post; } else { HttpPost post = new HttpPost(serverUrl); request = post; } } else if (remoteType == HTTP_GET) { if (hashMap != null) { for (Entry<String, String> entry : hashMap.entrySet()) { if (!serverUrl.contains("?")) serverUrl += "?" + entry.getKey() + "=" + entry.getValue(); else serverUrl += "&" + entry.getKey() + "=" + entry.getValue(); } } request = new HttpGet(serverUrl); } HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); HttpResponse httpResponse = httpClient.execute(request); int status = httpResponse.getStatusLine().getStatusCode(); if (status != 401) { entity = httpResponse.getEntity(); } return entity; }
二、将Json串转换为List集合
public static List<PhoneNumberModel> jsonToList(String json) { List<PhoneNumberModel> numberModels = new ArrayList<PhoneNumberModel>(); try { JSONObject jsonObject = new JSONObject(json); if (jsonObject.optBoolean("success", false)) { JSONArray jsonArray = jsonObject.getJSONArray("data"); PhoneNumberModel model; for (int i = 0; i < jsonArray.length(); i++) { JSONObject dcObject = jsonArray.getJSONObject(i) .getJSONObject("dc"); JSONObject cbObject = jsonArray.getJSONObject(i) .getJSONObject("cb"); model = new PhoneNumberModel(); model.setJuLi(dcObject.optDouble("distance")); model.setName(cbObject.optString("NAME1")); model.setNumber(cbObject.optString("AREA_CODE", "") + cbObject.optString("TEL", "")); model.setFrom(MyApplication.getMyApplication().getString( R.string.page_tag)); numberModels.add(model); } } } catch (JSONException e) { e.printStackTrace(); } return numberModels; }
相关推荐
json转换json schema
JSON转换为JAVA对象及日期格式转换处理 -
string与json转换需要的jar包
gson-2.3.1.jar json 转换工具
NULL 博文链接:https://gerrard-ok.iteye.com/blog/2017340
json转换工具类,可以转换很多格式。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
json转换所需全套jar包,包含json-lib-2.4-jdk15.jar、ezmorph-1.0.3.jar、commons-logging-1.0.4.jar、commons-lang-2.3.jar、commons-httpclient-3.1.jar、commons-collections-3.1.jar、commons-beanutils-1.7.0....
输入多层级的json后,默认处理最外层级,生成对应的SQL 建表脚本。若需要处理子层级json,可将子层级json重新放入到生成框内,最终再手动拼接脚本
uni-app、微信小程序 富文本内容利用html2json转换成对应类似控件 rich-text 对应的数组资源格式,进行富文本文件的展示。
json转换工具,很好,很强大的哦,欢迎各位亲们下载
对象与json相互转换,map转json,json转map等基础工具类
json2toml 将JSON转换为 。例子var json2toml = require ( 'json2toml' ) ;json2toml ( { simple : true } ) ;// => 'simple = true\n'// Also supports pretty-printing optionsjson2toml ( { deeply : { option : ...
el表达式,json转换和redis使用所需jar包含有gson-2.3.1.jar,jedis-2.9.0.jar和jstl-1.2.jar
Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (http://blog.csdn.net/lk_blog/article/details/7685169) Json转换利器Gson之实例二-Gson注解和GsonBuilder ...
json对象转换成实体,实体转换json字符串。常用数据类型转换(int、double、base64字符串等),发现问题及时联系我删除资源
前台请求后台通常需要JSON转换,所以调用自定以的类去转换很方便,转换类也是通过反射的机制实现的
JSON 对象转换 JSON转换 JSON解析 解析JSON 对象之间转换,与实体转换
json转换
实现json转换,fastjson json json转换,可以帮助开发中涉及json格式处理的程序猿。
NULL 博文链接:https://black-angel.iteye.com/blog/339722