使用Thread+HttpURLConntion+Handler

package com.example.slx.slx12_28;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {

    private EditText phone;
    private EditText psw;
    private Button login;
    private String path;

    private URL url;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //数据源
            Users obj = (Users) msg.obj;
            String code = obj.getCode();
            String msg1 = obj.getMsg();
            if (Integer.parseInt(code) == 0){
                Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                startActivity(intent);
            }else {
                Toast.makeText(MainActivity.this,msg1,Toast.LENGTH_SHORT).show();

            }



        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        phone = findViewById(R.id.phone);
        psw = findViewById(R.id.psw);
        login = findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View view) {
                String mobile = phone.getText().toString();
                String password = psw.getText().toString();
                path = "http://120.27.23.105/user/reg?mobile="+mobile+"&password="+password;


                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        requestNetData();
                    }
                }.start();



}
        });


}

    public void requestNetData(){
             //创建url对象
        try {
            URL url = new URL(path);
            //打开连接
            HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
            //进行设置
            urlConnection.setRequestMethod("GET");
            urlConnection.setReadTimeout(5000);
            urlConnection.setConnectTimeout(5000);
            //判断响应码
            int responseCode = urlConnection.getResponseCode();
            if (responseCode==200){
                //获取数据
                InputStream inputStream = urlConnection.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                int len=-1;
                while ((len=inputStream.read(bytes))!=-1){
                    byteArrayOutputStream.write(bytes,0,len);
                }

                String s = byteArrayOutputStream.toString();

                Gson gson = new Gson();
                Users users = gson.fromJson(s, Users.class);
                //发送信息给主线程
                Message obtain = Message.obtain();
                obtain.obj=users;
                handler.sendMessage(obtain);
            }

        } catch (
                Exception e) {
            e.printStackTrace();
        }
    }
}



package com.example.slx.slx12_28;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

/**
 * Created by lenovo on 2017/12/28.
 */

public class MyAdapter extends BaseAdapter{
    private Context context;
    private List<Goods.DataBean> list;

    public MyAdapter(Context context, List<Goods.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view==null){
            view=View.inflate(context,R.layout.itemlayout,null);
            holder=new ViewHolder();
            holder.textView=(TextView)view.findViewById(R.id.textview);
            holder.imageView=(ImageView)view.findViewById(R.id.imageview);
            view.setTag(holder);
        }else{
            holder=(ViewHolder)view.getTag();
        }
            holder.textView.setText(list.get(i).getTitle());
            MyPicTask myPicTask = new MyPicTask(holder.imageView);
            String[] split = list.get(i).getImages().split("\\|");
            myPicTask.execute(split[0]);


        return view;
    }

    public class ViewHolder{
        ImageView imageView;
        TextView textView;
    }

    class MyPicTask extends AsyncTask<String,Void,Bitmap>{
         private ImageView image;

        public MyPicTask(ImageView image) {
            this.image = image;
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setReadTimeout(5000);
                urlConnection.setConnectTimeout(5000);
                int responseCode = urlConnection.getResponseCode();
                if(responseCode==200){
                    InputStream inputStream =urlConnection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap==null){
                image.setImageResource(R.mipmap.ic_launcher);
            }else {
                image.setImageBitmap(bitmap);
            }
        }
    }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值