achref05 achref05 - 1 month ago 13
JSON Question

System.err: org.json.JSONException: Value

I'm developping web service application and im using rest api and i want test it with json.

when i run my application i have this error:


D/REQUEST Result: result =
{"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"}]}
W/System.err: org.json.JSONException: Value
{"result":[{"id":"1","duration":"achref","imageRes":"http://127.0.0.1/asd.jpg","mat":"achref","name":"achref"},{"id":"2","duration":"achref","imageRes":"http://127.0.0.1/asd.jpg","mat":"achref","name":"achref"},{"id":"3","duration":"achref","imageRes":"http://127.0.0.1/asd.jpg","mat":"achref","name":"achref"},{"id":"4","duration":"achref","imageRes":"http://127.0.0.1/asd.jpg","mat":"achref","name":"achref"},{"id":"5","duration":"achref","imageRes":"http://127.0.0.1/asd.jpg","mat":"achref","name":"achref"}]}
of type org.json.JSONObject cannot be converted to 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: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at
java.util.concurrent.FutureTask.run(FutureTask.java:237) W/System.err:
at 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); ?>


could help me?
thank you :)

this java file

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();

}

}

}


and this 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;

}
}

Answer

Modify the beginning of this method:

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

    try {

        JSONObject result = new JSONObject(json);
        JSONArray array = result.getJSONArray("result");

        // ...

UPDATE

Like @Arcao mentioned, imageRes is a string URL but Prisonner class seems to store it as an int. Try to change it to String and it may work.