日常生活小记

login 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.zpj.aaa.myapplication.Login">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="user"
    android:textSize="25sp"/>
    <EditText
        android:layout_width="304dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_name"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="password"
        android:textSize="25sp"/>
     <EditText
         android:layout_width="320dp"
         android:layout_height="wrap_content"
         android:id="@+id/rt_pwd"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="login"
        android:textAllCaps="false"
        android:id="@+id/button_login"/>
</LinearLayout>


main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zpj.aaa.myapplication.MainActivity">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ListView_lv"/>
</RelativeLayout>

prolist

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zpj.aaa.myapplication.Prolist">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ImageView_img"
    android:background="@mipmap/ic_launcher"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TextView_name"
            android:textSize="20sp"
            android:text="ss"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TextView_price"
            android:text="ss"
            android:textSize="20sp"/>
    </LinearLayout>
</LinearLayout>

Login 

public class Login extends AppCompatActivity implements View.OnClickListener {
    private EditText et_name, et_pwd;
    private Button bt_login;
    final int OK = 1;
    final int NO = -1;
    String stringurl = "http://49.122.47.4:81/wulian/m/Login";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_name = (EditText) findViewById(R.id.et_name);
        et_pwd = (EditText) findViewById(R.id.rt_pwd);
        bt_login = (Button) findViewById(R.id.button_login);
        bt_login.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        String username = et_name.getText().toString().trim();
        String userpassword = et_pwd.getText().toString().trim();
        if (username.length()>0&&userpassword.length()>0)
        {


            new MyThread().start();
            Toast.makeText(Login.this,"0000000",Toast.LENGTH_LONG).show();
        }else
        {
            Toast.makeText(Login.this,"请输入正确的用户名或者密码",Toast.LENGTH_LONG).show();
        }


    }


    MyHandler myHandler = new MyHandler();


    public class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case OK:
                    String username = et_name.getText().toString().trim();
                    SharedPreferences sp = getSharedPreferences("zhou",MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("user",username);
                    editor.commit();
                    Toast.makeText(Login.this, "success", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Login.this,MainActivity.class);
                    //跳转到商品页
                    Toast.makeText(Login.this, "正在为你跳转", Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    break;
                case NO:
                    Toast.makeText(Login.this, "//", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }


    MyThread myThread = new MyThread();


    public class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                String username = et_name.getText().toString().trim();
                String userpassword = et_pwd.getText().toString().trim();
                URL url = new URL(stringurl + "?username=" + username + "&userpassword=" + userpassword);
                Log.e("url::::",""+stringurl + "?username" + username + "&userpassword" + userpassword);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(2000);


                httpURLConnection.setRequestMethod("GET");
                InputStream inputStream = httpURLConnection.getInputStream();
                int code = httpURLConnection.getResponseCode();
                if (code == 200) {


                    String resquest = readStread(inputStream);
                    Log.e("zhong","登录结果为:"+resquest);


                    if (resquest.equals("1")) {
                        Message message =new Message();
                        message.what = OK;


                        myHandler.sendMessage(message);


                        Log.e("ooo","oooooo");




                    } else {
                        Message message = new Message();
                        message.what=NO;
                       // Toast.makeText(Login.this, "得到读取的readstream为0", Toast.LENGTH_SHORT).show();


                        myHandler.sendMessage(message);
                        Log.e("readstream:","得到读取的readstream为0");


                    }
                } else {
                    Log.e("ppppppppp","----------");
                   /* Message message=new Message();
                    message.what=NO;
                    myHandler.sendMessage(message);


                    Toast.makeText(Login.this, "请求超时", Toast.LENGTH_SHORT).show();*/
                }
            }
            catch (Exception ex)
            {
                ex.getStackTrace();
            }
        }
    }


    public static String readStread(InputStream is) {
        StringBuffer sb = new StringBuffer("");
        InputStream inputStream = is;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String temp;
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                br.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();


    }
}



MainActivity


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    final int Ok = 1;
    final int No = 0;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private ListView listView;


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


        String username;
        SharedPreferences sp = getSharedPreferences("zhou", MODE_PRIVATE);
        username = sp.getString("user", "");
        if (username.equals("")) {
            Intent intent = new Intent(MainActivity.this, Login.class);
            Toast.makeText(MainActivity.this, "用户没有登录", Toast.LENGTH_LONG).show();
            startActivity(intent);
        }


        listView = (ListView) findViewById(R.id.ListView_lv);
        Log.e("..............", "666666666");
        getDateFromSever();


    }


    public void getDateFromSever() {
        String url = "http://49.122.47.4:81/wulian/m/GetProduct";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("Mainactivty", "请求失败");
                Message message = new Message();
                message.what = No;
                myHandler.sendMessage(message);
            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("Mainactivity", "请求成功");
                if (response.isSuccessful()) {
                    String result = readStread(response.body().byteStream());


                    Gson gson = new Gson();
                    productBeanList = gson.fromJson(result, new TypeToken<List<ProductBean>>() {
                    }.getType());


                    Message message = new Message();
                    message.what = Ok;
                    myHandler.sendMessage(message);


                } else {
                    Toast.makeText(MainActivity.this, "响应数据失败", Toast.LENGTH_SHORT).show();


                }


            }
        });




    }




    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)


    {
        Intent intent = new Intent(MainActivity.this, Prolist.class);
        startActivity(intent);
    }


    MyHandler myHandler = new MyHandler();


    public class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Ok:
                    Toast.makeText(MainActivity.this, "读取数据成功转跳到下一个页面", Toast.LENGTH_SHORT).show();
                    //转跳
                    Log.e("..............", ".......");


                    Mybaseadapter mybaseadapter = new Mybaseadapter(MainActivity.this, productBeanList);
                    listView.setAdapter(mybaseadapter);
                    break;
                case No:
                    Toast.makeText(MainActivity.this, "读取数据失败,请检查错误", Toast.LENGTH_SHORT).show();


                    break;
            }


        }
    }


    public static String readStread(InputStream is) {
        StringBuffer sb = new StringBuffer("");
        InputStream inputStream = is;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String temp;
            while ((temp = br.readLine()) != null) {
                sb.append(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                br.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();


    }
}

adapter


public class Mybaseadapter extends BaseAdapter {
    Context context;
    List<ProductBean> productBeanList;


    public Mybaseadapter(Context context, List<ProductBean> productBeen) {
        this.context = context;
        this.productBeanList = productBeen;
    }


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


    @Override
    public Object getItem(int position) {
        return productBeanList.get(position);
    }


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


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.activity_prolist, null);


        }
        TextView tv_name = (TextView) convertView.findViewById(R.id.TextView_name);
        TextView tv_price = (TextView) convertView.findViewById(R.id.TextView_price);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.ImageView_img);
        String imgurl = "http://49.122.47.4:81/wulian/" + productBeanList.get(position).getProImg();
        Picasso.with(context)
                .load(imgurl)
                .into(imageView);


        Log.e("zzzzzzzzz", "imgurl=" + imgurl);
        return convertView;
    }
}


ProductBean 

public class ProductBean {
    int proID;
    String proName;


    public double getProMarketPrice() {
        return proMarketPrice;
    }


    public void setProMarketPrice(double proMarketPrice) {
        this.proMarketPrice = proMarketPrice;
    }


    public int getProID() {
        return proID;
    }


    public void setProID(int proID) {
        this.proID = proID;
    }


    public String getProName() {
        return proName;
    }


    public void setProName(String proName) {
        this.proName = proName;
    }


    public double getProPrice() {
        return proPrice;
    }


    public void setProPrice(double proPrice) {
        this.proPrice = proPrice;
    }


    public String getProImg() {
        return proImg;
    }


    public void setProImg(String proImg) {
        this.proImg = proImg;
    }


    double proMarketPrice; //市场价
    double proPrice;//商品价格
    String proImg;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Protobuf是一种高效的序列化协议,可以用于数据交换和数据存储。它的主要优势是大小小,速度快,可扩展性强。下面是使用Protobuf的一些小记: 1. 定义消息格式 首先,需要定义消息格式,以便Protobuf可以将数据序列化和反序列化。消息格式定义在.proto文件中,使用protobuf语言编写。例如,下面是一个简单的消息格式定义: ``` syntax = "proto3"; message Person { string name = 1; int32 age = 2; } ``` 这个消息格式定义了一个名为Person的消息,包含两个字段:name和age。 2. 生成代码 一旦消息格式定义好,就可以使用Protobuf编译器生成代码。编译器将根据消息格式定义生成相应的代码,包括消息类、序列化和反序列化方法等。可以使用以下命令生成代码: ``` protoc --java_out=. message.proto ``` 这将生成一个名为message.pb.java的Java类,该类包含Person消息的定义以及相关方法。 3. 序列化和反序列化 一旦生成了代码,就可以使用Protobuf序列化和反序列化数据。例如,下面是一个示例代码,将一个Person对象序列化为字节数组,并将其反序列化为另一个Person对象: ``` Person person = Person.newBuilder() .setName("Alice") .setAge(25) .build(); byte[] bytes = person.toByteArray(); Person deserializedPerson = Person.parseFrom(bytes); ``` 这个示例代码创建了一个Person对象,将其序列化为字节数组,然后将其反序列化为另一个Person对象。在这个过程中,Protobuf使用生成的代码执行序列化和反序列化操作。 以上是使用Protobuf的一些基本步骤和注意事项,希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值