Http
private String getHttpValue() { String str=""; String url = "http://test"; URL getUrl; HttpURLConnection connection; try { getUrl = new URL(url); connection = (HttpURLConnection) getUrl.openConnection(); connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { str+=lines; } reader.close(); connection.disconnect(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("请求返回内容:\n" + str.toString()); if (str != "") { try { JSONObject jsonObject = new JSONObject(str); str = jsonObject.getString("key"); } catch (Exception e) { Log.e(TAG, e.toString()); } } return str; }
http获取网络Json参数
private void setHttpImage() { ImageView image = null; try { Log.i(TAG, "http协议准备获取网络头像资源..."); globalInfoStatus = 0; globalVariables.SendMessageInfo = 0; byte[] data = getHttpImage(urlPath); if (data == null) { Log.i(TAG, "获取到网页头像资源失败"); } else { Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); image.setImageBitmap(bitmap); Log.i(TAG, "获取到网页头像资源"); } } catch (Exception e) { String error = "网络连接失败,请重试!"; // Toast.makeText(MainActivity.this, error, 1).show(); Log.e(TAG, e.toString()); } } /** httpGetImage根据路径获取图片二进制编码 */ public static byte[] getHttpImage(String path) throws Exception { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream(); return readFromInput(inStream); } catch (Exception e) { Log.i(TAG, e.toString()); return null; } }
http获取网络图片资源
https
获取网络参数
public String getURL(String userName) { String URLPath = "https://xxx&username=" + userName; String result = ""; String str = ""; try { URL url = new URL(URLPath); SSLContext sslctxt = SSLContext.getInstance("TLS"); sslctxt.init(null, new TrustManager[] { new MyX509TrustManager() }, new java.security.SecureRandom()); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(sslctxt.getSocketFactory()); conn.setHostnameVerifier(new MyHostnameVerifier()); conn.connect(); int respCode = conn.getResponseCode(); Log.d(TAG, "ResponseCode=" + respCode); if (respCode != 200) { return ""; } InputStream input = conn.getInputStream(); result = getInputString(input); Log.d(TAG, "result:" + result); input.close(); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } if (result != "") { try { JSONObject jsonObject = new JSONObject(result); str = jsonObject.getString("url"); } catch (Exception e) { Log.e(TAG, e.toString()); } } return str; }
获取json字符串中的value
static class MyX509TrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { if (null != chain) { for (int k = 0; k < chain.length; k++) { X509Certificate cer = chain[k]; print(cer); } } Log.d(TAG, "check client trusted. authType=" + authType); } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { if (null != chain) { for (int k = 0; k < chain.length; k++) { X509Certificate cer = chain[k]; print(cer); } } Log.d(TAG, "check servlet trusted. authType=" + authType); } @Override public X509Certificate[] getAcceptedIssuers() { Log.d(TAG, "get acceptedissuer"); return null; } private void print(X509Certificate cer) { int version = cer.getVersion(); String sinname = cer.getSigAlgName(); String type = cer.getType(); String algorname = cer.getPublicKey().getAlgorithm(); BigInteger serialnum = cer.getSerialNumber(); Principal principal = cer.getIssuerDN(); String principalname = principal.getName(); Log.d(TAG, "version=" + version + ", sinname=" + sinname + ", type=" + type + ", algorname=" + algorname + ", serialnum=" + serialnum + ", principalname=" + principalname); } } static class MyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { Log.d(TAG, "hostname=" + hostname + ",PeerHost= " + session.getPeerHost()); return true; } } private String getInputString(InputStream input) { String content = ""; try { InputStreamReader ir = new InputStreamReader(input); BufferedReader br = new BufferedReader(ir); StringBuilder sbuff = new StringBuilder(); while (null != br) { String temp = br.readLine(); if (null == temp) break; sbuff.append(temp).append(System.getProperty("line.separator")); } content = sbuff.toString(); // content=EncodingUtils.getString(baf.toByteArray(), "UTF-8"); content = URLDecoder.decode(content, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return content; }
各种需要写配合的方法类
Runnable runnableGetUserURL = new Runnable() { @Override public void run() {// 在新的线程中运行 if (globalVariables.UserName != "") { imageURLString = getURL(UserName); Log.d("MainActivity", "imageURL:" + imageURLString); } } }; mThread = new Thread(runnableGetUserURL); mThread.start();
获取参数的方法需要放入其他线程中
根据URL获取图片资源
public void setUserImage(String urlPath) { try { Log.i(TAG, "https协议准备获取网络头像资源..."); Bitmap bitmap = httpsGetBitmap(urlPath); image = (ImageView)findViewById(R.id.image); image.setImageBitmap(bitmap); } catch (Exception e) { String error = "网络连接失败,请重试!"; // Toast.makeText(MainActivity.this, error, 1).show(); Log.e(TAG, e.toString()); } } private Bitmap httpsGetBitmap(String URLPath) throws Exception { URL url = new URL(URLPath); SSLContext sslctxt = SSLContext.getInstance("TLS"); sslctxt.init(null, new TrustManager[] { new MyX509TrustManager() }, new java.security.SecureRandom()); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(sslctxt.getSocketFactory()); conn.setHostnameVerifier(new MyHostnameVerifier()); conn.connect(); InputStream inputSream = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputSream); inputSream.close(); conn.disconnect(); return bitmap; }
设置图片资源
时间: 2024-09-29 17:40:51