用户购物车是否为空展示不同界面

master
ghtMare 2 years ago
parent 6dd806b439
commit b0e2420b7f

@ -142,7 +142,7 @@ public class CatProductController {
file.transferTo(destFile); file.transferTo(destFile);
} }
map.put("flag", true); map.put("flag", true);
map.put("data", "http://10.21.120.101:8081/uploaded/" + fileName); map.put("data", "http://10.21.91.105:8081/uploaded/" + fileName);
return map; return map;
} catch (Exception e) { } catch (Exception e) {
map.put("flag", false); map.put("flag", false);

@ -88,7 +88,7 @@ public class CatShoppingCartController {
file.transferTo(destFile); file.transferTo(destFile);
} }
map.put("flag", true); map.put("flag", true);
map.put("data", "http://10.21.120.101:8081/uploaded/" + fileName); map.put("data", "http://10.21.91.105:8081/uploaded/" + fileName);
return map; return map;
} catch (Exception e) { } catch (Exception e) {
map.put("flag", false); map.put("flag", false);

@ -4,25 +4,34 @@ import android.app.Activity;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.example.catapp.R; import com.example.catapp.R;
import com.example.catapp.entity.CatProductPack;
import com.example.catapp.entity.ShoppingCartPack;
import com.example.catapp.fragment.IndexFragment; import com.example.catapp.fragment.IndexFragment;
import com.example.catapp.fragment.PearsonFragment; import com.example.catapp.fragment.PearsonFragment;
import com.example.catapp.fragment.ProductFragment; import com.example.catapp.fragment.ProductFragment;
import com.example.catapp.fragment.ShoppingCartFragment; import com.example.catapp.fragment.ShoppingCartFragment;
import com.example.catapp.netrequest.OkHttpShoppingcart;
import com.example.catapp.service.TimeService; import com.example.catapp.service.TimeService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class IndexActivity extends Activity implements View.OnClickListener { public class IndexActivity extends Activity implements View.OnClickListener {
private IndexFragment indexFragment; private IndexFragment indexFragment;
private ProductFragment productFragment; private ProductFragment productFragment;
private ShoppingCartFragment shoppingCartFragment; private ShoppingCartFragment shoppingCartFragment;
private PearsonFragment pearsonFragment; private PearsonFragment pearsonFragment;
private LinearLayout indexLine, productLine, shoppingCartLine, pearsonLine; private LinearLayout indexLine, productLine, shoppingCartLine, pearsonLine;
public static List<ShoppingCartPack> cartList = new ArrayList<>();
private Integer userId=1;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -31,6 +40,8 @@ public class IndexActivity extends Activity implements View.OnClickListener {
initIndexFragment(); initIndexFragment();
Thread thread = new Thread(runnable); Thread thread = new Thread(runnable);
thread.start(); thread.start();
check();//检查用户购物车是否为空
Log.d("InActivity get cartList.size() ",""+cartList.size());
} }
/** /**
@ -45,6 +56,7 @@ public class IndexActivity extends Activity implements View.OnClickListener {
shoppingCartLine.setOnClickListener(this); shoppingCartLine.setOnClickListener(this);
pearsonLine = findViewById(R.id.content_pearson); pearsonLine = findViewById(R.id.content_pearson);
pearsonLine.setOnClickListener(this); pearsonLine.setOnClickListener(this);
getUserId();
} }
@Override @Override
@ -65,6 +77,15 @@ public class IndexActivity extends Activity implements View.OnClickListener {
} }
} }
/**
* userId
*/
private void getUserId() {
Intent intent = IndexActivity.this.getIntent();
Bundle bundle = intent.getExtras();
userId=(Integer)bundle.getInt("id");
Log.d("IndexActivity.userId",""+userId);
}
/** /**
* Fragment * Fragment
*/ */
@ -128,4 +149,23 @@ public class IndexActivity extends Activity implements View.OnClickListener {
startService(intent); startService(intent);
}; };
/**
*
*/
public void check(){
new Thread(new Runnable() {
@Override
public void run() {
OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart();
try {
if(okHttpShoppingcart.getShoppingCartPack(userId)==null)return;
cartList=okHttpShoppingcart.getShoppingCartPack(userId);
Log.d("check()",""+(cartList.size()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
} }

@ -5,17 +5,21 @@ import android.app.FragmentTransaction;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button; import android.widget.Button;
import android.widget.GridView; import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.example.catapp.R; import com.example.catapp.R;
import com.example.catapp.activity.IndexActivity;
import com.example.catapp.adapter.ProductAdapter; import com.example.catapp.adapter.ProductAdapter;
import com.example.catapp.adapter.ShoppingCartAdapter; import com.example.catapp.adapter.ShoppingCartAdapter;
import com.example.catapp.entity.CatProductPack; import com.example.catapp.entity.CatProductPack;
@ -34,23 +38,50 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
private ProductFragment productFragment; private ProductFragment productFragment;
private GridView productGridView; private GridView productGridView;
private TextView usernameText; private TextView usernameText;
private Integer userId=2; Button left1;
private Integer userId=13;
private List<CatProductPack> catProductList= new ArrayList<>(); private List<CatProductPack> catProductList= new ArrayList<>();
Handler h=null; Handler h=null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//给购物车集合赋值,用以判断界面的展示
cartList=IndexActivity.cartList;
Log.d("ShoppingCartFragment onCrate init cartList",""+cartList);
}
// @Override
// public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// super.onActivityCreated(savedInstanceState);
// productGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// FragmentTransaction transaction = getFragmentManager().beginTransaction();
// if (productFragment == null) {
// productFragment = new ProductFragment();
// }
// transaction.replace(R.id.main_content0, productFragment);
// transaction.commit();
//
// }
// });
// }
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view; View view;
// if (cartList.size() > 0) { cartList=IndexActivity.cartList;
// //有商品 if (cartList.size() > 0) {
//有商品
view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false); view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false);
// } else { linkHttp(view);
// view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false); } else {
// } view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false);
}
init(view); init(view);
check();
// if (cartList.size() > 0)
linkHttp();
return view; return view;
} }
@ -59,12 +90,15 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
*/ */
private void init(View view) { private void init(View view) {
usernameText=view.findViewById(R.id.username); usernameText=view.findViewById(R.id.username);
productGridView = view.findViewById(R.id.cart_productList); //productGridView = view.findViewById(R.id.cart_productList);
if (cartList.size() > 0) {
// walkButton = view.findViewById(R.id.random_search); }
// walkButton.setOnClickListener(this); else {
walkButton = view.findViewById(R.id.random_search);
walkButton.setOnClickListener(this);
}
//productGridView = view.findViewById(R.id.cart_productList);
setData(); setData();
} }
private void setData() { private void setData() {
@ -75,7 +109,9 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
} }
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (cartList.size() > 0) {} if (cartList.size() > 0) {
}
else { else {
switch (v.getId()) { switch (v.getId()) {
case R.id.random_search: case R.id.random_search:
@ -91,29 +127,11 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
} }
} }
/** /**
* *
*/ */
public void check(){ public void linkHttp(View view){
new Thread(new Runnable() {
@Override
public void run() {
OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart();
try {
// catProductList= okHttpShoppingcart.getProductPack(userId);
System.out.println(cartList.size() );
cartList=okHttpShoppingcart.getShoppingCartPack(userId);
System.out.println(cartList.get(0).getNum());
System.out.println(cartList.size());
System.out.println(cartList.size());
System.out.println(cartList.size());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
public void linkHttp(){
h =new Handler() { h =new Handler() {
@Override @Override
public void handleMessage(@NonNull Message msg) { public void handleMessage(@NonNull Message msg) {
@ -128,8 +146,9 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
public void run() { public void run() {
OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart(); OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart();
try { try {
productGridView = view.findViewById(R.id.cart_productList);
catProductList= okHttpShoppingcart.getProductPack(userId); catProductList= okHttpShoppingcart.getProductPack(userId);
//cartList=okHttpShoppingcart.getShoppingCartPack(userId); cartList=okHttpShoppingcart.getShoppingCartPack(userId);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

@ -20,6 +20,7 @@ import okhttp3.Response;
public class OkHttpClientProduct { public class OkHttpClientProduct {
public static String Url="http://10.21.91.105:8081";
/** /**
* *
* *
@ -29,7 +30,7 @@ public class OkHttpClientProduct {
OkHttpClient okHttpClient = new OkHttpClient(); OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url("http://10.21.120.101:8081/orange/product/search") .url(Url+"/orange/product/search")
.build(); .build();
Response response = okHttpClient.newCall(request).execute(); Response response = okHttpClient.newCall(request).execute();

@ -22,7 +22,7 @@ public class OkHttpMessage {
OkHttpClient okHttpClient = new OkHttpClient(); OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
//.url("http://192.168.0.102:8081/orange/message/consumeMessage") //.url("http://192.168.0.102:8081/orange/message/consumeMessage")
.url("http://10.21.120.101:8081/orange/message/consumeMessage") .url(OkHttpClientProduct.Url+"/orange/message/consumeMessage")
.build(); .build();
Response response = okHttpClient.newCall(request).execute(); Response response = okHttpClient.newCall(request).execute();
JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string()); JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(response.body()).string());
@ -31,7 +31,7 @@ public class OkHttpMessage {
* *
*/ */
Request request1 = new Request.Builder() Request request1 = new Request.Builder()
.url("http://10.21.120.101:8081/orange/message/updateMessage") .url(OkHttpClientProduct.Url+"/orange/message/updateMessage")
.build(); .build();
okHttpClient.newCall(request1).execute(); okHttpClient.newCall(request1).execute();
return orangeMessage; return orangeMessage;

@ -2,6 +2,7 @@ package com.example.catapp.netrequest;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.util.Log;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
@ -23,13 +24,13 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
public class OkHttpShoppingcart { public class OkHttpShoppingcart {
//shoppingCart
/** /**
* *
* * @param userId id
* @return * @return
* @throws IOException
*/ */
public List<CatProduct> getProduct(Integer userId) throws IOException { public List<CatProduct> getProduct(Integer userId) throws IOException {
OkHttpClient okHttpClient = new OkHttpClient(); OkHttpClient okHttpClient = new OkHttpClient();
//数据类型为json格式 //数据类型为json格式
@ -39,7 +40,7 @@ public class OkHttpShoppingcart {
RequestBody requestBody = RequestBody.create(jsonStr, mediaType); RequestBody requestBody = RequestBody.create(jsonStr, mediaType);
Request request = new Request.Builder() Request request = new Request.Builder()
.url("http://10.21.120.101:8081/orange/shoppingcart/search1") .url(OkHttpClientProduct.Url+"/orange/shoppingcart/search1")
.post(requestBody) .post(requestBody)
.build(); .build();
Response response = okHttpClient.newCall(request).execute(); Response response = okHttpClient.newCall(request).execute();
@ -52,6 +53,12 @@ public class OkHttpShoppingcart {
return list; return list;
} }
/**
*
* @param userId id
* @return
* @throws IOException
*/
public List<ShoppingCart> getShoppingCart(Integer userId) throws IOException { public List<ShoppingCart> getShoppingCart(Integer userId) throws IOException {
OkHttpClient okHttpClient = new OkHttpClient(); OkHttpClient okHttpClient = new OkHttpClient();
@ -61,22 +68,16 @@ public class OkHttpShoppingcart {
String jsonStr = JSONObject.toJSONString(userId); String jsonStr = JSONObject.toJSONString(userId);
RequestBody requestBody = RequestBody.create(jsonStr, mediaType); RequestBody requestBody = RequestBody.create(jsonStr, mediaType);
Request request = new Request.Builder() Request request = new Request.Builder()
.url("http://10.21.120.101:8081/orange/shoppingcart/sharecropping") .url(OkHttpClientProduct.Url+"/orange/shoppingcart/sharecropping")
.post(requestBody) .post(requestBody)
.build(); .build();
Response response = okHttpClient.newCall(request).execute(); Response response = okHttpClient.newCall(request).execute();
System.out.println(response.isSuccessful()); Log.d("getShoppingCart request is :",""+response.isSuccessful());
System.out.println(response.isSuccessful());
String responseStr = response.body().string(); String responseStr = response.body().string();
JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(responseStr)); JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(responseStr));
List<ShoppingCart> list = JSON.parseObject(jsonObject.getString("data"), new TypeReference<List<ShoppingCart>>() { List<ShoppingCart> list = JSON.parseObject(jsonObject.getString("data"), new TypeReference<List<ShoppingCart>>() {
}); });
System.out.println(list.get(0).getNum()); Log.d("list",""+(list!=null));
System.out.println(list.get(0).getNum());
System.out.println(list.get(0).getNum());
System.out.println(list.get(0).getNum());
System.out.println(list.get(0).getNum());
return list; return list;
} }
@ -100,17 +101,17 @@ public class OkHttpShoppingcart {
} }
/** /**
* *
* * @param userId
* @return * @return
* @throws IOException * @throws IOException
*/ */
public List<CatProductPack> getProductPack(Integer userId) throws IOException { public List<CatProductPack> getProductPack(Integer userId) throws IOException {
List<CatProductPack> packList = new ArrayList<>(); List<CatProductPack> packList = new ArrayList<>();
List<CatProduct> list = getProduct(userId); List<CatProduct> list = getProduct(userId);
if(list==null)return null;
for (CatProduct catProduct : list) { for (CatProduct catProduct : list) {
CatProductPack catProductPack = new CatProductPack(); CatProductPack catProductPack = new CatProductPack();
catProductPack.setId(catProduct.getId()); catProductPack.setId(catProduct.getId());
@ -129,9 +130,17 @@ public class OkHttpShoppingcart {
return packList; return packList;
} }
/**
*
* @param userId
* @return
* @throws IOException
*/
public List<ShoppingCartPack> getShoppingCartPack(Integer userId) throws IOException { public List<ShoppingCartPack> getShoppingCartPack(Integer userId) throws IOException {
List<ShoppingCartPack> packList = new ArrayList<>(); List<ShoppingCartPack> packList = new ArrayList<>();
List<ShoppingCart> list = getShoppingCart(userId); List<ShoppingCart> list = getShoppingCart(userId);
if(list==null)return null;
else {
for (ShoppingCart catProduct : list) { for (ShoppingCart catProduct : list) {
ShoppingCartPack catProductPack = new ShoppingCartPack(); ShoppingCartPack catProductPack = new ShoppingCartPack();
catProductPack.setId(catProduct.getId()); catProductPack.setId(catProduct.getId());
@ -139,7 +148,8 @@ public class OkHttpShoppingcart {
catProductPack.setProductId(catProduct.getProductId()); catProductPack.setProductId(catProduct.getProductId());
catProductPack.setNum(catProduct.getNum()); catProductPack.setNum(catProduct.getNum());
packList.add(catProductPack); packList.add(catProductPack);
System.out.println(6666666);
} }
return packList; return packList;}
} }
} }

@ -29,7 +29,7 @@ public class OkHttpUser {
RequestBody requestBody = RequestBody.create(jsonStr, mediaType); RequestBody requestBody = RequestBody.create(jsonStr, mediaType);
Request request = new Request.Builder() Request request = new Request.Builder()
.url("http://10.21.120.101:8081/orange/user/login") .url(OkHttpClientProduct.Url+"/orange/user/login")
.post(requestBody) .post(requestBody)
.build(); .build();
@ -58,7 +58,7 @@ public class OkHttpUser {
String jsonStr = JSONObject.toJSONString(catUser); String jsonStr = JSONObject.toJSONString(catUser);
RequestBody requestBody = RequestBody.create(jsonStr, mediaType); RequestBody requestBody = RequestBody.create(jsonStr, mediaType);
Request request = new Request.Builder() Request request = new Request.Builder()
.url("http://10.21.120.101:8081/orange/user/register") .url(OkHttpClientProduct.Url+"/orange/user/register")
.post(requestBody) .post(requestBody)
.build(); .build();
Response response = okHttpClient.newCall(request).execute(); Response response = okHttpClient.newCall(request).execute();

@ -3,7 +3,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#14FFEB3B" android:background="#14FFEB3B"
android:orientation="vertical"> android:orientation="vertical"
>
<ImageView <ImageView
android:id="@+id/category_product_image" android:id="@+id/category_product_image"
@ -34,16 +35,18 @@
android:gravity="center" android:gravity="center"
android:textColor="#050505" android:textColor="#050505"
android:textSize="16sp" /> android:textSize="16sp" />
<Button
<ImageView
android:id="@+id/left" android:id="@+id/left"
android:layout_width="30dp" android:layout_width="48dp"
android:layout_height="30dp" android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginLeft="40dp" android:layout_marginLeft="40dp"
android:layout_marginTop="55dp" android:layout_marginTop="55dp"
android:layout_toRightOf="@id/category_product_price" android:layout_toRightOf="@id/category_product_price"
android:src="@drawable/left" /> android:background="@drawable/left"
android:text="-"
android:textColor="#070707"
android:textSize="18sp"
/>
<TextView <TextView
android:id="@+id/num" android:id="@+id/num"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -55,14 +58,17 @@
android:background="#00FEFEFE" android:background="#00FEFEFE"
android:textColor="#050505" android:textColor="#050505"
android:textSize="16sp" /> android:textSize="16sp" />
<ImageView <Button
android:id="@+id/right" android:id="@+id/right"
android:layout_width="40dp" android:layout_width="48dp"
android:layout_height="40dp" android:layout_height="48dp"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginLeft="20dp" android:layout_marginLeft="20dp"
android:layout_marginTop="50dp" android:layout_marginTop="50dp"
android:layout_toRightOf="@id/num" android:layout_toRightOf="@id/num"
android:src="@drawable/right" /> android:background="@drawable/right"
android:text="+"
android:textColor="#1C1C1C"
android:textSize="18sp"/>
</RelativeLayout> </RelativeLayout>
Loading…
Cancel
Save