传值和交互视觉设计

object 的内部即开始标签(start tag)”” 和“

public class MainActivity9 extends AppCompatActivity {
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main9);

        ImageView iv=findViewById(R.id.iv);
        Button bt2=findViewById(R.id.bt2);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url=new URL("http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=12");
                            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                            connection.setConnectTimeout(15000);
                            connection.setReadTimeout(15000);
                            connection.connect();
                            if (connection.getResponseCode()==200){
                                InputStream is=connection.getInputStream();
                                Bitmap bitmap= BitmapFactory.decodeStream(is);
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        iv.setImageBitmap(bitmap);
                                    }
                                });
                                is.close();
                            }
                        } catch (MalformedURLException e) {
                            throw new RuntimeException(e);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
            }
        });

        Button bt=findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url=new URL("http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=12");
                            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("POST");
                            connection.setConnectTimeout(5000);
                            connection.setReadTimeout(5000);
                            connection.setDoOutput(true);
                            connection.connect();
                            String data="username=123&password=123";
                            connection.getOutputStream().write(data.getBytes());
                            if (connection.getResponseCode()==200){
                                InputStream is=connection.getInputStream();
                                BufferedReader br=new BufferedReader(new InputStreamReader(is));
                                StringBuilder sb=new StringBuilder();
                                String line=null;
                                while ((line=br.readLine())!=null){
                                    sb.append(line);
                                }
                                JsonResult jr=new Gson().fromJson(sb.toString(),JsonResult.class);
                                System.out.println(jr.getMessage()+"-"+jr.getCode());
                            }
                        } catch (MalformedURLException e) {
                            throw new RuntimeException(e);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();
            }
        });
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值