收到错误:com.android.volley.ParseError:org.json.JSONException:值<br of =“” type =“” java.lang.string =“”不能=“” be =“”转换=“”为=“” jsonarray =“”

时间:2019-05-06 20:03:10

标签: java php android json api

=“”

我尝试通过API从数据库中获取数据,但是recyclerview不显示任何内容,并且日志显示错误。

这是我的代码

片段

    package com.example.hp.retailmakanan;

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.design.widget.BottomNavigationView;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;

    import com.android.volley.RequestQueue;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.hp.retailmakanan.Model.MenuModel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class FragHome extends Fragment {
    RecyclerView recV;
    BottomNavigationView top_navigation;
    HomeAdapter listAdapter;
    List<MenuModel>  listMenu= new ArrayList<>();


    private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            Fragment sfragment = null;
            switch (menuItem.getItemId()) {
                case R.id.nav_noodle:
                    recV.setAdapter(listAdapter);
                    break;
                case R.id.nav_beverage:
                    recV.setAdapter(listAdapter);
                    break;
                case R.id.nav_toping:
                    recV.setAdapter(listAdapter);
                    break;
                case R.id.nav_cari:
                    recV.setAdapter(listAdapter);
                    break;
            }
            return false;
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.act_home, container, false);
        recV = (RecyclerView) v.findViewById(R.id.rec_ramen);
        getData();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recV.setLayoutManager(layoutManager);
        listAdapter = new HomeAdapter(listMenu, getActivity());
        recV.setAdapter(listAdapter);
        top_navigation = (BottomNavigationView) v.findViewById(R.id.top_nav);
        top_navigation.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
        return v;

    }

这是我用来获取数据的代码

    private void getData() {
        String url ="https://waggish-requisition.000webhostapp.com/GetAllMenu.php";
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                listMenu.clear();
                for (int i = 0; i < response.length(); i++) {
                    try {
                        Log.d("menu",response.toString());
                        JSONObject jsonObject = response.getJSONObject(i);
                        String id_menu = jsonObject.getString("id");
                        String nama_menu = jsonObject.getString("nama_menu");
                        String deskripsi = jsonObject.getString("deskripsi");
                        int harga = jsonObject.getInt("harga");

                        MenuModel menus = new MenuModel(id_menu,nama_menu,deskripsi,harga);
                        listMenu.add(menus);
                        System.out.println(menus);
                        //Toast.makeText(this,"Download data "+i,Toast.LENGTH_SHORT).show();

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.d("menu",e.toString());
                    }
                    listAdapter.notifyDataSetChanged();

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volley", error.toString());
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(jsonArrayRequest);
    }

}

适配器

package com.example.hp.retailmakanan;

import android.content.Context;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hp.retailmakanan.Model.MenuModel;

import java.util.Arrays;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter {

    private List<MenuModel> menus;
    private Context context;

    public HomeAdapter(List<MenuModel> menus, Context context) {
        this.context = context;
        this.menus = menus;
        //System.out.println(menus);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recv_desain, viewGroup, false);
        return new ListViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int pos) {

    }

    @Override
    public int getItemCount() {
        return menus.size();
    }



    public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView mText,txt2, txt3, txtQty;
        ImageView img;
        Button btnMin, btnPlus;
        int x;

        public ListViewHolder(View itemView){
            super(itemView);
            mText = itemView.findViewById(R.id.text_item);
            txt2 = itemView.findViewById(R.id.text_item2);
            txt3 = itemView.findViewById(R.id.textView3);
            img = itemView.findViewById(R.id.image_item);
            btnMin = itemView.findViewById(R.id.buttonMinMenu);
            btnPlus = itemView.findViewById(R.id.buttonPlusMenu);
            txtQty = itemView.findViewById(R.id.textQtyMenu);
            itemView.setOnClickListener(this);
        }

        public void bindView (int pos){
            x=0;
            MenuModel model = menus.get(pos);
            mText.setText(model.getNama_menu());
            txt2.setText(model.getDeskripsi());
            txt3.setText(model.getHarga());
            //img.setImageResource(DataMenu.picture[pos]);

            txtQty.setText(String.valueOf(x));
            btnPlus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    x++;
                    txtQty.setText(String.valueOf(x));

                }
            });

            btnMin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    x--;
                    if (x<=0){
                        x=0;
                        txtQty.setText(String.valueOf(x));
                    }else if (x>0){
                        txtQty.setText(String.valueOf(x));
                    }
                }
            });

        }

        @Override
        public void onClick(View v) {

        }
    }
}

这是我的php代码

<?php
    require_once('connection.php');
    //public $mServerName;
    //public $mConnectionInfo;
    //public $conn;





        //Create query to retrieve all contacts
        $query = "SELECT * FROM menu";

        $stmt = mysqli_query($conn, $query);

        if (!$stmt)
        {
            //Query failed
            echo 'Query failed';
        }

        else
        {
            $menus= array(); //Create an array to hold all of the contacts
            //Query successful, begin putting each contact into an array of contacts

            while ($row = mysqli_fetch_array($stmt)) //While there are still contacts
            {
                //Create an associative array to hold the current contact
                //the names must match exactly the property names in the contact class in our C# code.
                $menu= array("id" => $row['id_menu'],
                                 "deskripsi" => $row['Deskripsi'],
                                 "nama_menu" => $row['nm_menu'],
                                 "harga" => $row['harga_menu'],
                                 "ImageUrl" =>$row['gambar']
                                 );

                //Add the contact to the contacts array
                array_push($menus, $menu);
            }

            //Echo out the contacts array in JSON format

            echo json_encode($menus);

        }

?>

我将JSONObject更改为JSONArray,但这导致了许多其他错误。我是初学者,请帮助我。我真的不知道该怎么办:(

0 个答案:

没有答案