救救可怜的孩子吧
package com.example.connection; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private static final int GET_DATA_SUCCESS =1 ; private static final int SERVER_ERROR =2; private Button button; private ImageView imageView; private DiglettHandler mHandler = new DiglettHandler(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button); imageView=(ImageView)findViewById(R.id.image_view); button.setOnClickListener(this); } public void onClick(View v){ switch (v.getId()){ case R.id.button: new GetPicThread().start(); break; } } public static class DiglettHandler extends Handler { public final WeakReference<MainActivity> mWeakReference; public DiglettHandler(MainActivity activity){ mWeakReference = new WeakReference<>(activity); } public void handleMessage(Message msg) { super.handleMessage(msg); MainActivity activity = mWeakReference.get(); Bitmap bitmap = (Bitmap) msg.obj; activity.imageView.setImageBitmap(bitmap); } } public class GetPicThread extends Thread implements Runnable { public void run() { try { //获取请求的url地址 URL url = new URL("https://img1.sycdn.imooc.com//5adfee7f0001cbb906000338-240-135.jpg"); //获得URL请求连接的对象 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置超时时间 connection.setConnectTimeout(100 * 1000); //设置请求的方式 connection.setRequestMethod("GET"); //传输数据包格式 connection.setRequestProperty("Content0-Type", "application/json"); //传输数据的格式 connection.setRequestProperty("Charset", "UTF-8"); connection.setRequestProperty("Accept-Charset", "UTF-8"); connection.connect();//发起连接 int resopnseCode = connection.getResponseCode(); String responseMessage = connection.getResponseMessage(); if (resopnseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); //使用工厂把网络的输入流生产Bitmap Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//利用Message把图片发给Handler Message msg = Message.obtain(); msg.obj = bitmap; msg.what = GET_DATA_SUCCESS; mHandler.sendMessage(msg); inputStream.close(); } else { //服务启发生错误 mHandler.sendEmptyMessage(SERVER_ERROR); } } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } 老师我点了按钮后图片不显示,我已经添加了网络权限了 报错信息: 2019-10-23 12:24:15.807 12409-18176/? E/ResolverController: No valid NAT64 prefix (100, <unspecified>/0) 2019-10-23 12:24:15.822 18127-18175/com.example.connection W/System.err: java.net.SocketException: socket failed: EPERM (Operation not permitted) 2019-10-23 12:24:15.822 18127-18175/com.example.connection W/System.err: at java.net.Socket.createImpl(Socket.java:492) 2019-10-23 12:24:15.822 18127-18175/com.example.connection W/System.err: at java.net.Socket.getImpl(Socket.java:552) 2019-10-23 12:24:15.822 18127-18175/com.example.connection W/System.err: at java.net.Socket.setSoTimeout(Socket.java:1180) 2019-10-23 12:24:15.822 18127-18175/com.example.connection W/System.err: at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143) 2019-10-23 12:24:15.824 18127-18175/com.example.connection W/System.err: at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116) 2019-10-23 12:24:15.824 18127-18175/com.example.connection W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186) 2019-10-23 12:24:15.824 18127-18175/com.example.connection W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
11
收起
正在回答 回答被采纳积分+1
1回答
2.Android 网络操作与流行框架
- 参与学习 人
- 提交作业 220 份
- 解答问题 1800 个
本阶段是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理;从文件存储、到轻量级数据库SQLite的使用;最后利用最流程的第三方框架助你快速开发企业级APP。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星