System.err:org.json.JSONException:Value

时间:2016-10-11 16:46:54

标签: android json rest android-studio

我正在开发Web服务应用程序,我正在使用rest api,我想用json进行测试。

当我运行我的应用程序时,我有这个错误:

  

D / REQUEST结果:结果=   { “结果”:[{ “ID”: “1”, “名称”: “achref”, “垫”: “achref”, “持续时间”: “achref”, “imageRes”:“HTTP://127.0。 0.1 / asd.jpg “},{” ID “:” 2" , “名称”: “achref”, “垫”: “achref”, “持续时间”: “achref”, “imageRes”:“HTTP:// 127.0.0.1/asd.jpg"},{"id":"3","name":"achref","mat":"achref","duration":"achref","imageRes":"http: //127.0.0.1/asd.jpg"},{"id":"4","name":"achref","mat":"achref","duration":"achref","imageRes “:” http://127.0.0.1/asd.jpg"},{"id":"5","name":"achref","mat":"achref","duration":"achref","imageRes” : “http://127.0.0.1/asd.jpg”}]}   W / System.err:org.json.JSONException:Value   { “结果”:[{ “ID”: “1”, “时间”: “achref”, “imageRes”: “http://127.0.0.1/asd.jpg”, “垫”: “achref”,”命名 “:” achref “},{” ID “:” 2" , “持续时间”: “achref”, “imageRes”: “http://127.0.0.1/asd.jpg”, “垫”: “achref” , “姓名”: “achref”},{ “ID”: “3”, “持续时间”: “achref”, “imageRes”: “http://127.0.0.1/asd.jpg”, “垫”:” achref”, “名称”: “achref”},{ “ID”: “4”, “持续时间”: “achref”, “imageRes”: “http://127.0.0.1/asd.jpg”, “垫” : “achref”, “名称”: “achref”},{ “ID”: “5”, “持续时间”: “achref”, “imageRes”: “http://127.0.0.1/asd.jpg”,”垫 “:” achref”, “名”: “achref”}]}   类型org.json.JSONObject无法转换为JSONArray   W / System.err:at org.json.JSON.typeMismatch(JSON.java:111)   W / System.err:at org.json.JSONArray。(JSONArray.java:96)   W / System.err:at org.json.JSONArray。(JSONArray.java:108)   W / System.err:at   com.example.wiola.prisonapp.fragments.PrisonnerListFragment.getPrisonersFromJson(PrisonnerListFragment.java:166)   W / System.err:at   com.example.wiola.prisonapp.fragments.PrisonnerListFragment $ AsyncGetPrisoners.doInBackground(PrisonnerListFragment.java:103)   W / System.err:at   com.example.wiola.prisonapp.fragments.PrisonnerListFragment $ AsyncGetPrisoners.doInBackground(PrisonnerListFragment.java:87)   W / System.err:在android.os.AsyncTask $ 2.call(AsyncTask.java:288)   W / System.err:at   java.util.concurrent.FutureTask.run(FutureTask.java:237)W / System.err:   在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)   W / System.err:at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   W / System.err:at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)   W / System.err:at java.lang.Thread.run(Thread.java:841)

and php file

>  <?php define('HOST','localhost'); define('USER','root');
> define('PASS',''); define('DB','achrefDB');   $con =
> mysqli_connect(HOST,USER,PASS,DB);   $sql = "select * from Prisonner";
> $res = mysqli_query($con,$sql);   $result = array();   while($row =
> mysqli_fetch_array($res)){ array_push($result, array('id'=>$row[0],
> 'name'=>$row[1], 'mat'=>$row[2], 'duration'=>$row[3],
> 'imageRes'=>$row[4]   
> 
> )); }   echo json_encode(array("result"=>$result));  
> mysqli_close($con);   ?>

可以帮到我吗? 谢谢:)

这个java文件

public class PrisonnerListFragment extends Fragment {

    ListView lvprisonners;
    private PrisonnerAdapter adapter;
    private ArrayList<Prisonner> prisoners;
    private ProgressDialog dialog;

    public PrisonnerListFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_prisonner_list, container, false);
        lvprisonners = (ListView) view.findViewById(R.id.lv_prisonners);

        new AsyncGetPrisoners().execute();


        return view;
    }

    class AsyncGetPrisoners extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            prisoners = new ArrayList<>();
            dialog = new ProgressDialog(getActivity());
            dialog.setTitle("In Progress");
            dialog.setMessage("En cours du traitement");
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            String jsonStream = request("http://192.168.0.100/connexion.php").toString();
            getPrisonersFromJson(prisoners, jsonStream);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);


            PrisonnerAdapter adapter = new PrisonnerAdapter(getActivity(), R.layout.item_prisonner, prisoners);

            lvprisonners.setAdapter(adapter);

            lvprisonners.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    getFragmentManager()
                            .beginTransaction()
                            .replace(R.id.container,
                                    PrisonnerDetailFragment.newInstance(prisoners.get(position)))
                            .addToBackStack(null).commit();
                }
            });


            dialog.dismiss();
        }

    }

    public StringBuffer request(String urlString) {

        StringBuffer result = new StringBuffer("");
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("root", "");
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));

            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("REQUEST Result", "IOException = " + e.getMessage());
        }
        Log.d("REQUEST Result", "result = " + result);
        return result;

    }

    public void getPrisonersFromJson(List<Prisonner> prisoners,String json) {

        try {
            JSONArray array = new JSONArray(json);
            for (int i = 0; i < array.length(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);

                Prisonner p;
                p = new Prisonner();

                p.setId(jsonObject.optInt("id"));
                p.setName(jsonObject.optString("name"));
                p.setMat(jsonObject.optString("mat"));
                p.setDuration(jsonObject.optString("duration"));
                p.setImageRes(jsonObject.optInt("imageRes"));


                prisoners.add(p);

            }
        } catch (JSONException e) {
            e.printStackTrace();

        }

    }

}

和这个json

{"result":[{"id":"1","name":"achref","mat":"achref","duration":"achref","imageRes":"http:\/\/127.0.0.1\/asd.jpg"},{"id":"2","name":"achref","mat":"achref","duration":"achref","imageRes":"http:\/\/127.0.0.1\/asd.jpg"},{"id":"3","name":"achref","mat":"achref","duration":"achref","imageRes":"http:\/\/127.0.0.1\/asd.jpg"},{"id":"4","name":"achref","mat":"achref","duration":"achref","imageRes":"http:\/\/127.0.0.1\/asd.jpg"},{"id":"5","name":"achref","mat":"achref","duration":"achref","imageRes":"http:\/\/127.0.0.1\/asd.jpg"}]}

prisonnerAdapter

public class PrisonnerAdapter extends ArrayAdapter<Prisonner> {

    Context context;
    int resource;
    public PrisonnerAdapter(Context context, int resource, List<Prisonner> prisonners) {
        super(context, resource, prisonners);
        this.context=context;
        this.resource=resource;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        PrisonnerHolder holder = new PrisonnerHolder();

        if (view==null){

        LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view =inflater.inflate(resource, parent, false);



            holder.TvName = (TextView) view.findViewById(R.id.tv_prisonner_name);
            holder.TvMat = (TextView) view.findViewById(R.id.tv_prisonner_mat);
            holder.TvDuration = (TextView) view.findViewById(R.id.tv_prisonner_duration);
            //holder.ImgPrisonner = (ImageView) view.findViewById(R.id.img_prisonner);
            Picasso.with(context).load(getItem(position).getImageRes()).into(holder.ImgPrisonner);
            view.setTag(holder);
        }
        else{
          holder = (PrisonnerHolder) view.getTag();
        }


        holder.TvName.setText(getItem(position).getName());
        holder.TvMat.setText(getItem(position).getMat());
        holder.TvDuration.setText(getItem(position).getDuration());
        holder.ImgPrisonner.setBackgroundResource(getItem(position).getImageRes());

        return view;
    }

    class PrisonnerHolder {
        TextView TvName;
        TextView TvMat;
        TextView TvDuration;
        ImageView ImgPrisonner;

    }
}

2 个答案:

答案 0 :(得分:2)

要访问对象,请使用

 JSONArray resultArray = result.getJSONArray("result");
 for(int i = 0; i < resultArray.length(); i++) {
                JSONObject obj = resultArray.getJSONObject(i);
                //store your variable
                String name = obj.getString("name");
 }

了解更多详情 见https://developer.android.com/reference/org/json/JSONObject.html

答案 1 :(得分:1)

修改此方法的开头:

var caNCourbeData = [{
    "mois": 1,
    "nbFactures": 2,
    "nbFacturesReglees": 1,
    "nbdossiersRealise": 1,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 1256.92,
    "id": 0
}, {
    "mois": 2,
    "nbFactures": 4,
    "nbFacturesReglees": 2,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 413.00,
    "id": 1
}, {
    "mois": 3,
    "nbFactures": 3,
    "nbFacturesReglees": 3,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 30.00,
    "id": 2
}, {
    "mois": 4,
    "nbFactures": 0,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "id": 3
}, {
    "mois": 5,
    "nbFactures": 0,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "id": 4
}, {
    "mois": 6,
    "nbFactures": 9,
    "nbFacturesReglees": 1,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 2253.31,
    "id": 5
}, {
    "mois": 7,
    "nbFactures": 0,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 2,
    "nbContratConverti": 0,
    "id": 6
}, {
    "mois": 8,
    "nbFactures": 0,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "id": 7
}, {
    "mois": 9,
    "nbFactures": 0,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "id": 8
}, {
    "mois": 10,
    "nbFactures": 2,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 450.60,
    "id": 9
}, {
    "mois": 11,
    "nbFactures": 0,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "id": 10
}, {
    "mois": 12,
    "nbFactures": 0,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "id": 11
}];


var caN_1CourbeData = [{
    "mois": 1,
    "nbFactures": 36,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 3,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": -141559.33,
    "id": 0
}, {
    "mois": 2,
    "nbFactures": 144,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 23,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 19662.80,
    "id": 0
}, {
    "mois": 3,
    "nbFactures": 39,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 10835.42,
    "id": 0
}, {
    "mois": 4,
    "nbFactures": 30,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 2,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 12166.74,
    "id": 0
}, {
    "mois": 5,
    "nbFactures": 73,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 1,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 19391.04,
    "id": 0
}, {
    "mois": 6,
    "nbFactures": 36,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 5,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 23929.57,
    "id": 0
}, {
    "mois": 7,
    "nbFactures": 24,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 1,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 5446.59,
    "id": 0
}, {
    "mois": 8,
    "nbFactures": 24,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 3019.31,
    "id": 0
}, {
    "mois": 9,
    "nbFactures": 20,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 3178.76,
    "id": 0
}, {
    "mois": 10,
    "nbFactures": 1,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 0,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 10.00,
    "id": 0
}, {
    "mois": 11,
    "nbFactures": 14,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 2,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 6020.56,
    "id": 0
}, {
    "mois": 12,
    "nbFactures": 21,
    "nbFacturesReglees": 0,
    "nbdossiersRealise": 6,
    "nbContrat": 0,
    "nbContratConverti": 0,
    "chiffreAffaire": 4709.05,
    "id": 0
}];

<强>更新

与@Arcao提到的一样,public void getPrisonersFromJson(List<Prisonner> prisoners, String json) { try { JSONObject result = new JSONObject(json); JSONArray array = result.getJSONArray("result"); // ... 是字符串网址,但imageRes类似乎将其存储为Prisonner。尝试将其更改为int,它可能会有效。