当前位置: 首页 > 知识库问答 >
问题:

使用Jsonreader setLenient(true)接受格式错误的JSON

廖琪
2023-03-14

我试图解决这个错误,但我做不到。

public class Reservation_main extends AppCompatActivity {
final String TAG = "ReservationMainActivity";
public static final String url = "http://10.0.2.2:81/PhpProject1/room.php";
RecyclerView recycle;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reserve_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recycle = (RecyclerView)findViewById(R.id.recycle);
    recycle.setHasFixedSize(true);

    LinearLayoutManager manager = new LinearLayoutManager(this);

    recycle.setLayoutManager(manager);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d(TAG, response);

                    ArrayList<Reservation_room_list> roomList = new JsonConverter<Reservation_room_list>()
                            .toArrayList(response, Reservation_room_list.class);

                    Reservation_room_adapter adapter = new Reservation_room_adapter(getApplicationContext(), roomList);

                    recycle.setAdapter(adapter);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(error != null){
                        Log.d(TAG, error.toString());
                        Toast.makeText(getApplicationContext(), "Something went wrong.", Toast.LENGTH_LONG).show();
                    }
                }
            }
    );

    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);


}
}
public class Reservation_room_adapter extends 
RecyclerView.Adapter<Reservation_room_html" target="_blank">adapter.RoomViewHolder> {

private Context context;
private ArrayList<Reservation_room_list> room;

public Reservation_room_adapter(Context context, ArrayList<Reservation_room_list> room){
    this.context = context;
    this.room = room;
}

@Override
public RoomViewHolder onCreateViewHolder (ViewGroup parent, int type){
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.from(parent.getContext())
            .inflate(R.layout.activity_reserve_layout, parent, false);

    RoomViewHolder roomViewHolder = new RoomViewHolder(view);
    return roomViewHolder;
}

@Override
public void onBindViewHolder (RoomViewHolder holder, int position){
    Reservation_room_list rooms = room.get(position);
    holder.roomNo.setText(rooms.roomid);
    holder.price.setText("" + rooms.price);
    holder.roomDescription.setText(rooms.type);

    String fullUrl = "http://10.0.2.2:81/PhpProject1/" + rooms.image;

    Picasso.with(context)
            .load(fullUrl)
            .placeholder(R.drawable.bed_room_web)
            .error(android.R.drawable.stat_notify_error)
            .into(holder.roomImage);
}

@Override
public int getItemCount(){
    if (room != null){
        return room.size();
    }
    return 0;
}

public static class RoomViewHolder extends RecyclerView.ViewHolder{
    public CardView info;
    public TextView roomNo;
    public TextView roomDescription;
    public TextView price;
    public ImageView roomImage;

    public RoomViewHolder(View roomView){
        super(roomView);
        info = (CardView) roomView.findViewById(R.id.view);
        roomNo = (TextView) roomView.findViewById(R.id.room);
        roomDescription = (TextView) roomView.findViewById(R.id.details);
        price = (TextView) roomView.findViewById(R.id.price);
        roomImage = (ImageView) roomView.findViewById(R.id.imageView2);
    }
}

}
public class Reservation_room_list implements Serializable{
public String type;
public String image;
public String status;
public int roomid;
public int capacity;
public double price;
}
<?php
 error_reporting(0);
  require 'index.php';

 $query = "SELECT * FROM `room` WHERE Status = 'available' ORDER BY 
  Room_number desc";
  $response["sucsess"] = false;

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

while($row = mysqli_fetch_assoc($result)){
    $response["success"] = true;
        $data[] = $row;
}

echo json_encode($data);

?>
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;

private MySingleton(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized MySingleton getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new MySingleton(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = 
    Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}
}

运行时得到的错误

09-03 07:45:34.436 602-6020/fyp.hotelreservationsystemver2 e/recyclerview:没有连接适配器;跳过布局09-03 07:45:34.465 602-6020/fyp.hotelreservationsystemver2 D/reservationmainactivity:[{“Room_Number”:“1”,“Type”:“Luxury”,“Capacity”:“10”,“Status”:“Available”,“Price”:“1000”,“Image”:“img/hotel-hooper-decor-hotel-style-bedrooms.jpg”}]/**若要更改此许可证标头,请在项目属性中选择许可证标头。*要更改此模板文件,请选择工具模板*并在编辑器中打开模板。*/09-03 07:45:34.504 602-6020/fyp.hotelreservationsystemver2 d/androidruntime:关闭VM 09-03 07:45:34.504 602-6020/fyp.hotelreservationsystemver2 e/androidruntime:致命异常:main process:fyp.hotelreservationsystemver2,pid:6020 com.google.gson.jsonsyntaxexception:com.google.gson.stream.malformedjsonexception:jsonreader.setlenient(true)在.gson.gson.fromjson(Gson.java:769)在com.google.gson.gson.fromjson(Gson.java:717)在com.kosalgeek.android.JSON.jsonconverter.toArrayList(jsonconverter.java:42)在fyp.hotelreservationSystemver2.reservation_main$1.1.onRespon(reservation_main.java:53)在fyp.hotelreservationSystemver2.reservation_main$1.onRespon(reservation_main.java:44)在stringrequest.java:30)在COM。Android.volley.ExecutorDelivery$responseDeliveryRunnable.run(executorDelivery.java:99)在Android.os.handler.handleCallback(handler.java:751)在Android.os.handler.dispatchMessage(handler.java:95),android.os.handler.dispatchMessage(handler.java:154)在Android.app.activityThread.loop(handler.java:154)在Android.app.activityThread.main(Al.os.zygoteinit.main(zygoteinit.java:756)由:com.google.gson.stream.malformedJSONException:使用jsonReader.setlenient(true)在com.google.gson.stream.jsonReader.syntaxerrer(jsonReader.java:1505)在com.google.gson.stream.jsonReader.checklenient(jsonReader.java:1386)在527)在com.google.gson.stream.jsonreader.peek(jsonreader.java:414)在com.google.gson.gson.AssertFullConsumm(gson.java:775)在com.google.gson.gson.fromjson(gson.java:769) 在com.google.gson.gson.fromjson(Gson.java:717) 在com.kosalgeek.android.json.jsonconverter.ToArrayList(jsonconverter.java:42) 在fyp.hotelreservationsystemver2.reservation_main$1.onrespons(reservation_main.java:53) 在fyp.hotelreservationsystemver2.reservation_main$1.onrespons(reservation_main.java:44) 在com.android.volley.toolbox.stringRequest.DeliverResponse(StringRequest.java:60) 在com.android.volley.toolbox.stringRequest.DeliverResponse(stringRequest.java:30) 在com.android.volley.executordelivery$responseDeliveryRunnable.run(executorDelivery.java:99) 在Android.os.handler.handleCallback(handler.java:751) 在Android.os.handler.DispatchMessage(Handler.java:95) 在Android.os.looper.loop(looper.java:154) 在Android.app.activitythread.main(activitythread.java:6077) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:866) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:756) 

我使用的gson和JSONconverter:

[1]: https://drive.google.com/open?id=0B9c7Ta0Lo2OVVkkyU1RJVzQ5bTQ

共有1个答案

韩靖琪
2023-03-14

[{“Room_Number”:“1”,“Type”:“Luxury”,“Capacity”:“10”,“Status”:“Available”,“Price”:“1000”,“Image”:“IMG/Hotel-Room-Decor-Hotel-Style-Bedrooms.jpg”}]/**若要更改此许可证标头,请在项目属性中选择许可证标头。*要更改此模板文件,请选择工具模板*并在编辑器中打开模板。*/

似乎是响应,简单地说,以某种方式附加到响应上的注释块使得响应不是有效的JSON。由于文本不出现在给定的代码中,因此它必须来自应用程序的其他地方。由于响应是错误的,错误一定在PHP代码中。

 类似资料: