定义一个有参方法,参数包含path还有handler不知道怎么处理
package com.example.felix.myapplication; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; 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.URL; public class HomewActivity extends AppCompatActivity implements View.OnClickListener { private Button button; private ImageView imageView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_homework); initView(); } private void initView() { button = findViewById(R.id.getPicture_button); imageView = findViewById(R.id.getPicture_imageView); button.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.getPicture_button: GetPicThread(); break; } } private void GetPicThread() { new Thread(new Runnable() { @Override public void run() { try { URL url=new URL("https://img1.sycdn.imooc.com//5adfee7f0001cbb906000338-240-135.jpg"); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.setConnectTimeout(30*1000); connection.setRequestMethod("GET"); connection.connect(); int responseCode=connection.getResponseCode(); if(responseCode==HttpURLConnection.HTTP_OK){ InputStream inputStream=connection.getInputStream(); //getPicture(inputStream,); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } /*private void getPicture(String path,Handler mhandler){ }*/ public static class mHandler extends Handler{ final WeakReference<HomewActivity> mWeakReference; public mHandler(HomewActivity activity) { mWeakReference=new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); HomewActivity activity=mWeakReference.get(); if(msg.what==1001){ } } } }
写成这样,麻烦老师指导一下有参方法的处理
0
收起
正在回答 回答被采纳积分+1
2回答
Felixxx
2018-11-22 11:08:25
package com.example.felix.myapplication; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; 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.URL; public class HomewActivity extends AppCompatActivity implements View.OnClickListener { public static final String PATH = "https://img1.sycdn.imooc.com//5adfee7f0001cbb906000338-240-135.jpg"; public static final int INT = 1001; private Button button; private ImageView imageView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_homework); initView(); } private void initView() { button = findViewById(R.id.getPicture_button); imageView = findViewById(R.id.getPicture_imageView); button.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.getPicture_button: GetPicThread(PATH, com.example.felix.myapplication.HomewActivity.mHandler); break; } } private void GetPicThread(final String path, final Handler handler) { new Thread(new Runnable() { @Override public void run() { try { URL url= new URL(path); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.setConnectTimeout(30*1000); connection.setRequestMethod("GET"); connection.connect(); int responseCode=connection.getResponseCode(); if(responseCode==HttpURLConnection.HTTP_OK){ InputStream inputStream=connection.getInputStream(); Message message=Message.obtain(); message.obj=inputStream; message.what=INT; handler.sendMessage(message); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } public static class mHandler extends Handler{ final WeakReference<HomewActivity> mWeakReference; public mHandler(HomewActivity activity) { mWeakReference=new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); HomewActivity activity=mWeakReference.get(); if(msg.what== INT){ activity.imageView.setImageBitmap((Bitmap) msg.obj); } } } }
Android网络操作与数据存储2018版
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星