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;
}