@Override public void onSuccess(ByteArrayBuffer baf) { try { String jsonString = EncodingUtils.getString(baf.toByteArray(), "utf8"); Log.d("URLHelper", "JSON response = " + jsonString); JSONObject obj = null; if (!jsonString.equals("") && !jsonString.equals("[]")) obj = new JSONObject(jsonString); mDelegate.updateModelWithJSONObject(obj, this.serviceId); } catch (Exception e) { e.printStackTrace(); mDelegate.connectionFailed(this.serviceId); } }
// 从文件中读取数据,保存到数据库中 public String readFileData(String fileName) { String result = ""; InputStream in; try { in = getResources().getAssets().open(fileName); int len = in.available(); byte[] buffer = new byte[len]; in.read(buffer); result = EncodingUtils.getString(buffer, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; }
public static String post(String url, Map<String, String> requestParameters) { HttpURLConnection connection = null; InputStream is = null; BufferedInputStream bis = null; try { byte[] parametersUrlByte = initRequestParameters(requestParameters); URL requestUrl = new URL(url); connection = (HttpURLConnection) requestUrl.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); connection.setDoOutput(true); connection.getOutputStream().write(parametersUrlByte, 0, parametersUrlByte.length); connection.getOutputStream().flush(); connection.getOutputStream().close(); is = connection.getInputStream(); bis = new BufferedInputStream(is); ByteArrayBuffer bab = new ByteArrayBuffer(32); int current = 0; while ((current = bis.read()) != -1) { bab.append((byte) current); } return EncodingUtils.getString(bab.toByteArray(), HTTP.UTF_8); } catch (Exception e) { LOGGER.error(e); return null; } finally { try { if (bis != null) { bis.close(); } if (is != null) { is.close(); } } catch (IOException e) { LOGGER.error(e); } if (connection != null) { connection.disconnect(); } } }
public void setPageData(JSONObject jsonObject) { String templateStr = null; try { InputStream in = getResources().getAssets().open(fileName); // 获取文件的字节数 int lenght = in.available(); // 创建byte数组 byte[] buffer = new byte[lenght]; // 将文件中的数据读到byte数组中 in.read(buffer); templateStr = EncodingUtils.getString(buffer, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } if (fileName.equals("about.html")) { // templateStr = templateStr.replace("$about", // jsonObject.getString("aboutContent").toString()); templateStr = templateStr.replace("$about", getString(R.string.version_maohao, getVersionName())); } if (fileName.equals("version.html")) { String versionName = getVersionName(); templateStr = templateStr.replace("$version", versionName); } Logger.i("temp", templateStr); ProxyBridge pBridge = new ProxyBridge(); webView.addJavascriptInterface(pBridge, "ProxyBridge"); if (webView != null) { webView.setWebViewClient( new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); mProgressDialog = null; } } }); webView.loadDataWithBaseURL(null, templateStr, "text/html", "utf-8", null); } }
public void setHost(Context context) throws IOException { SharedPreferences settings = context.getSharedPreferences("T4Config.cfg", 0); host = settings.getString("Host", defaultHost); String configPath = T4Function.rootFolderPath() + "/T4Config.cfg"; File file = new File(configPath); if (file.exists()) { FileInputStream fis = new FileInputStream(file); int length = fis.available(); byte[] bytes = new byte[length]; fis.read(bytes); fis.close(); String host = EncodingUtils.getString(bytes, "UTF-8"); String prefix = "http://"; if (!host.startsWith(prefix)) { host = prefix + host; } this.setHost(context, host); } }
public static String get(String url, String contentType) { HttpURLConnection connection = null; InputStream is = null; BufferedInputStream bis = null; try { URL requestUrl = new URL(url); connection = (HttpURLConnection) requestUrl.openConnection(); if (contentType != null) { connection.setRequestProperty("Content-type", contentType); } connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); is = connection.getInputStream(); bis = new BufferedInputStream(is); ByteArrayBuffer bab = new ByteArrayBuffer(32); int current = 0; while ((current = bis.read()) != -1) { bab.append((byte) current); } return EncodingUtils.getString(bab.toByteArray(), HTTP.UTF_8); } catch (Exception e) { LOGGER.error(e); return null; } finally { try { if (bis != null) { bis.close(); } if (is != null) { is.close(); } } catch (IOException e) { LOGGER.error(e); } if (connection != null) { connection.disconnect(); } } }
private boolean sendSavePasswordRequest() { message = null; boolean isSuccessful = false; try { String urlParams = setupAndCheckParams(); if (message == null) { URL url = new URL(CCWChinaConst.WEBSITE_CONTEXT + "/mobile/edit-password.htm?" + urlParams); InputStream inputStream = url.openStream(); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = inputStream.read()) != -1) { baf.append((byte) current); } String xml = EncodingUtils.getString(baf.toByteArray(), "UTF-8"); isSuccessful = parseSavePasswordXMl(xml); } } catch (Exception e) { e.printStackTrace(); message = CCWChinaConst.APP_ERROR_MSG; } return isSuccessful; }
/** * 将字符数组转换成字符串 * * @param buffer * @param charSet * @return */ public String byteArrayToString(byte[] buffer, String charSet) { return EncodingUtils.getString(buffer, charSet); }