`

Json转换

 
阅读更多

一、获取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;
	}

  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics