android--(http协议、显示网络中的图片、https)
来源:互联网 发布:淘宝优惠活动时间 编辑:程序博客网 时间:2024/06/06 01:56
https:
android httpClient 支持HTTPS的访问方式 - http://mobile.51cto.com/aprogram-447714.htm
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void checkNet(View view) { boolean newWork = isNetworkConnected(this); boolean wifi = isWiffiConnected(this); boolean mobile = isMobileConnected(this); int t = getConnectedType(this); if (newWork) { Toast.makeText(this, "网络可用", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "网络不可用", Toast.LENGTH_SHORT).show(); } if (wifi) { Toast.makeText(this, "wifi可用", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "wifi不可用", Toast.LENGTH_SHORT).show(); } if (mobile) { Toast.makeText(this, "mobile可用", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "mobile不可用", Toast.LENGTH_SHORT).show(); } Toast.makeText(this, "网络类型:"+t, Toast.LENGTH_SHORT).show(); } /** * 判断网络是否可用 * * @param context * @return */ public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null) { return networkInfo.isAvailable(); } } return false; } /** * 判断 wifi 是否可用 * * @param context * @return */ public boolean isWiffiConnected(Context context) { if (context != null) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); //根据网络的类型,获取相就类型的网络信息 NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifi != null) { return wifi.isAvailable(); } } return false; } /** * 判断 Mobile 网络是不可用 * * @param context * @return */ public boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobileNet = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mobileNet != null) { return mobileNet.isAvailable(); } } return false; } /** * 获取当前网络链接的类型信息 * * @param context * @return */ public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isAvailable()) { return networkInfo.getType(); } } return -1; }}
/** * 访问的网络必须在 线程中完成,显示网络中的图片 */public class Img extends AppCompatActivity { private static ImageView imageView; private final MyHandler myHandler = new MyHandler(this); private static final int LOAD_SUCESS = 0x1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_img); imageView = (ImageView) findViewById(R.id.imageView); showImg(); } /** * 线程之间进行通信处理 */ private static class MyHandler extends Handler { //软引用对象 private final WeakReference<Img> weakReference; public MyHandler(Img img) { this.weakReference = new WeakReference<Img>(img); } @Override public void handleMessage(Message msg) { Img img = weakReference.get(); if (img != null) { switch (msg.what) { case LOAD_SUCESS: // 在图片视图中显示 图片 imageView.setImageBitmap((Bitmap) msg.obj); break; } } } } /** * 网络显示图片 需要在线程中进行,防止网络阻塞 */ public void showImg() { new Thread(new Runnable() { @Override public void run() { String path = "http://img2.imgtn.bdimg.com/it/u=2655910209,3235656411&fm=21&gp=0.jpg"; try { URL url = new URL(path); InputStream in = url.openStream(); //以什么形式解码 Bitmap bitmap = BitmapFactory.decodeStream(in); //数据存入 handler的message Message message = myHandler.obtainMessage(LOAD_SUCESS, bitmap); //发送 myHandler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } }).start(); }}activity.xml <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Img" android:label="@string/title_activity_img" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
public class HttpUrl extends AppCompatActivity { private TextView tv_info; private EditText et_username, et_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http_url); tv_info = (TextView) findViewById(R.id.tv_info); et_username = (EditText) findViewById(R.id.editText_username); et_password = (EditText) findViewById(R.id.editText_password); } public void logIn(View view) { String username = et_username.getText().toString(); String password = et_password.getText().toString(); if (TextUtils.isEmpty(username)) { Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show(); return; } new Thread(new Runnable() { @Override public void run() { //这个路径为本地的 web服务器应用路径 String path = "http://10.0.2.2:8080/"; try { URL url = new URL(path); //建立http链接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //请求方式 conn.setRequestMethod("POST"); //因为是post请求 conn.setDoOutput(true); //因为是HttpURLConnection conn.setDoInput(true); //链接超时 时间毫秒 conn.setConnectTimeout(1000 * 30); //数据超时 conn.setReadTimeout(1000 * 30); //不缓存 conn.setUseCaches(false); //请求的属性 conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); //对服务器端读取或写入数据(使用输入输出流) //获取链接的输出流 DataOutputStream out = new DataOutputStream(conn.getOutputStream()); //往服务端写入参数数据 out.writeBytes("username=" + URLEncoder.encode(et_username.getText().toString(), "gbk")); out.writeBytes("&password=" + URLEncoder.encode(et_password.getText().toString(), "gbk")); out.flush(); out.close(); /***********从服务端获取数据***********************/ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));//字节流转字符流 String result = br.readLine(); br.close(); conn.disconnect();//关闭http链接 } catch (Exception e) { e.printStackTrace(); } } }).start(); }}
//先将参数放入List,再对参数进行URL编码 List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); params.add(new BasicNameValuePair("param1", "中国")); params.add(new BasicNameValuePair("param2", "value2")); //对参数编码 String param = URLEncodedUtils.format(params, "UTF-8"); //baseUrl String baseUrl = "http://ubs.free4lab.com/php/method.php"; //将URL与参数拼接 HttpGet getMethod = new HttpGet(baseUrl + "?" + param); HttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = httpClient.execute(getMethod); //发起GET请求 Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码 Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容 } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
/和GET方式一样,先将参数放入List params = new LinkedList<BasicNameValuePair>(); params.add(new BasicNameValuePair("param1", "Post方法")); params.add(new BasicNameValuePair("param2", "第二个参数")); try { HttpPost postMethod = new HttpPost(baseUrl); postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中 HttpResponse response = httpClient.execute(postMethod); //执行POST方法 Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码 Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容 } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
1 0
- android--(http协议、显示网络中的图片、https)
- HTTP和HTTPS网络协议
- Android利用HTTP协议加载网络图片
- Android显示网络中的图片
- Android网络编程之TCP/IP,HTTP,HTTPS协议了解
- Android webview加载https网页时http图片无法显示
- android http /https协议学习
- ios网络协议从http变成https
- http和https及网络协议
- Android开发教程:使用http协议获取网络图片
- 显示网络中的图片
- 【Android】WebView链接为https,内容图片为http,图片不显示
- android中的Http协议
- android 网络协议等socket,http,HTTPS,get。post请求 等详解
- Android 黑客入门 Http与Https协议
- Android 网络图片显示
- Android显示网络图片
- android 网络图片显示
- Android Studio 简介及导入 jar 包和第三方开源库方法
- VBS教程:创建txt文件
- easyui datebox 清空值或添加自定义按钮
- leetcode 257 : Binary Tree Paths
- web认证机制
- android--(http协议、显示网络中的图片、https)
- Word文档打不开怎么办
- js判断访问终端端
- c编程之代码优化总结
- Swift(一、基本入门)
- gstreamer android
- centos6 搭建pptp vpn注意
- linux 里的`反引号
- 十张图解释机器学习的基本概念