我在RecyclerView
中使用volley库进行JSON解析以从服务器获取数据,但ProgressBar
一直在运行且没有数据到来
我的PHP脚本很好,因为当我从其他活动发送数据时,我检查它是否正在更新
public class LiveFeeds extends AppCompatActivity {
List<DataAdapter> DataAdapterClassList;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
ProgressBar progressBar;
JsonArrayRequest jsonArrayRequest ;
ArrayList<String> SubjectNames;
RequestQueue requestQueue ;
String HTTP_SERVER_URL = "http://ledeveloper.in/bit/getbiddata.php";
View ChildView ;
int RecyclerViewClickedItemPOS ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_feeds);
DataAdapterClassList = new ArrayList<>();
SubjectNames = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
// JSON data web call function call from here.
JSON_WEB_CALL();
//RecyclerView Item click listener code starts from here.
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
GestureDetector gestureDetector = new GestureDetector(LiveFeeds.this, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent motionEvent) {
return true;
}
});
@Override
public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {
ChildView = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if(ChildView != null && gestureDetector.onTouchEvent(motionEvent)) {
//Getting RecyclerView Clicked item value.
RecyclerViewClickedItemPOS = Recyclerview.getChildAdapterPosition(ChildView);
//Printing RecyclerView Clicked item clicked value using Toast Message.
// Toast.makeText(LiveFeeds.this, SubjectNames.get(RecyclerViewClickedItemPOS), Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
}
public void JSON_WEB_CALL(){
jsonArrayRequest = new JsonArrayRequest(HTTP_SERVER_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
DataAdapter GetDataAdapter2 = new DataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setId(json.getInt("id"));
GetDataAdapter2.setName(json.getString("username"));
//Adding subject name here to show on click event.
// SubjectNames.add(json.getString("name"));
GetDataAdapter2.setPrice(json.getString("price"));
GetDataAdapter2.setQuantity(json.getString("quan"));
GetDataAdapter2.setTotal(json.getString("total"));
GetDataAdapter2.setTime(json.getString("bidtime"));
GetDataAdapter2.setDate(json.getString("bidbdate"));
}
catch (JSONException e)
{
e.printStackTrace();
}
DataAdapterClassList.add(GetDataAdapter2);
}
progressBar.setVisibility(View.GONE);
recyclerViewadapter = new RecyclerViewAdapter(DataAdapterClassList, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}
这是我的Adapter类 公共类RecyclerViewAdapter扩展了RecyclerView.Adapter {
Context context;
List<DataAdapter> dataAdapters;
public RecyclerViewAdapter(List<DataAdapter> getDataAdapter, Context context){
super();
this.dataAdapters = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
DataAdapter dataAdapter = dataAdapters.get(position);
viewHolder.ID.setText(dataAdapter.getId());
viewHolder.USERNAME.setText(String.valueOf(dataAdapter.getName()));
viewHolder.PRICE.setText(dataAdapter.getPrice());
viewHolder.QUANTITY.setText(dataAdapter.getQuantity());
viewHolder.TOTAL.setText(dataAdapter.getTotal());
viewHolder.TIME.setText(dataAdapter.getTime());
viewHolder.DATE.setText(dataAdapter.getDate());
}
@Override
public int getItemCount() {
return dataAdapters.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ID,USERNAME,PRICE,QUANTITY,TOTAL,TIME,DATE;
public ViewHolder(View itemView) {
super(itemView);
ID = (TextView) itemView.findViewById(R.id.textView2) ;
USERNAME = (TextView) itemView.findViewById(R.id.showusername) ;
PRICE = (TextView) itemView.findViewById(R.id.showprice) ;
QUANTITY = (TextView) itemView.findViewById(R.id.showquantt) ;
TOTAL = (TextView) itemView.findViewById(R.id.showtotal) ;
TIME = (TextView) itemView.findViewById(R.id.showtime) ;
DATE = (TextView) itemView.findViewById(R.id.showdate) ;
}
}
}
这是我的数据绑定类
public class DataAdapter {
int Id;
String username;
String price;
String quantity;
String total;
String time;
String date;
public String getName() {
return username;
}
public void setName(String name) {
this.username = name;
}
public int getId() {
return Id;
}
public void setId(int Id1) {
this.Id = Id1;
}
public String getPrice() {
return price;
}
public void setPrice(String pprice) {
this.price = pprice;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quant) {
this.quantity = quant;
}
public String getTotal() {
return total;
}
public void setTotal(String tot) {
this.total = tot;
}
public String getTime() {
return time;
}
public void setTime(String timm) {
this.time = timm;
}
public String getDate() {
return date;
}
public void setDate(String datt) {
this.date = datt;
}
}
这是我的php脚本
$respone = array();
if(isset($_GET['username'])){
$username = $_GET['username'];
$query = "SELECT * FROM bidtable ORDER BY id DESC ";
$result = mysqli_query($conn,$query);
if(mysqli_num_rows($result)>0)
{
while( $row = mysqli_fetch_assoc($result))
{
$product = array();
$product["id"] = $row["id"];
$product["username"] = $row["username"];
$product["price"] = $row["price"];
$product["quan"] = $row["quan"];
$product["total"] = $row["total"];
$product["bidtime"] = $row["bidtime"];
$response["success"] = "true";
$response["product"] = array();
array_push($response["product"], $product);
echo json_encode($response);
}
}
else{
$response["success"] = "false";
$response["message"] = "Something went wrong";
echo json_encode($response);
}
}
else{
$response["success"] = "false";
$response["message"] = "Required data not set.";
echo json_encode($response);
}
?>
答案 0 :(得分:0)
您未在请求中传递用户名参数。在URL中传递用户名参数,如下所示:
http://ledeveloper.in/bit/getbiddata.php?username=<username>
另外,在onErrorResponse
中添加此行progressBar.setVisibility(View.GONE);