错误:类型不兼容:字符串无法转换为JSONArray

时间:2019-09-13 22:08:34

标签: android

我是android开发的初学者。我正在尝试从带有Volley库的Web服务获取数据,但出现此错误

  

错误:类型不兼容:字符串无法转换为JSONArray。

模型类:

package com.example.carwashplaces;

public class ModelLocation {
    private String name, staff, latitude, longitude, id, phone, comment;

    public ModelLocation() {

    }

    public ModelLocation(String name, String staff, String latitude,String    longitude, String id, String phone, String comment) {
        this.name = name;
        this.staff = staff;
        this.latitude = latitude;
        this.longitude = longitude;
        this.id = id;
        this.phone = phone;
        this.comment = comment;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStaff() {
        return staff;
    }

    public void setStaff(String staff) {
        this.staff = staff;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
}

任务类:

package com.example.carwashplaces;

import android.content.Context;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.google.gson.JsonObject;


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

import java.util.ArrayList;

public class BackgroundTask {

    private Context context;
    private ArrayList<ModelLocation> locationArrayList = new ArrayList<>();
    String json_url = "http://developer.byethost14.com/location.php";

    public BackgroundTask(Context context) {
        this.context = context;
    }

    public ArrayList<ModelLocation> getLocationArrayList() {
        JsonArrayRequest jsonArrayRequest = new   JsonArrayRequest(Request.Method.POST, json_url, (String) null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                       int count = 0;
                       while (count < response.length()){
                           try {
                               JSONObject jsonObject =    response.getJSONObject(count);
                               ModelLocation modelLocation = new   ModelLocation(jsonObject.getString("id"),
                                       jsonObject.getString("name"),
                                       jsonObject.getString("latitude"),
                                       jsonObject.getString("longitude"),
                                       jsonObject.getString("staff"),
                                       jsonObject.getString("comment"),
                                       jsonObject.getString("phone"));
                               locationArrayList.add(modelLocation);
                               count++;
                           } catch (JSONException e) {
                               e.printStackTrace();
                           }
                       }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context, "Error...",        Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });

        MySingleton.getInstance(context).addToRequestque(jsonArrayRequest);

        return locationArrayList;
    }
}

JSON:

[
   {
      "id":"1",
      "name":"Nakuru",
      "latitude":"-0.28333",
      "longitude":"36.06667",
      "staff":"Elias Baya",
      "phone":"712695820",
      "comment":"Have a capacity of five cars at a go"
   },
   {
      "id":"2",
      "name":"Nairobi station",
      "latitude":"-1.28333",
      "longitude":"36.81667",
      "staff":"Elias David",
      "phone":"796268509",
      "comment":"Second car wash to be launched in 2000"
   },
   {
      "id":"3",
      "name":"Nyeri Station",
      "latitude":"-0.437099",
      "longitude":"36.95801",
      "staff":"Faith Neema",
      "phone":"726774112",
      "comment":"one of our best car wash"
   },
   {
      "id":"4",
      "name":"Mombasa station",
      "latitude":"-4.05466",
      "longitude":"39.66359",
      "staff":"Neema Elias",
      "phone":"710723308",
      "comment":"welcome all to mombasa station"
   },
   {
      "id":"5",
      "name":"Malindi Station",
      "latitude":"-3.21799",
      "longitude":"40.11692",
      "staff":"Baya Elias",
      "phone":"720038783",
      "comment":"launched in 2010 december"
   }
]

1 个答案:

答案 0 :(得分:0)

谢谢你们,代码没有问题。这是byethost.com故意添加的一项额外保护,以阻止bot访问我们的网站,从而为脚本提供了巨大的安全性。我将这些文件重新上传到000webhost.com,它的工作原理很吸引人。有关更多信息,请参阅此讨论.ByetHost服务器使用JSON字符串传递html值“检查浏览器”