-2

I heve a json url: http://wach.ma/mobile/item.php?id=15236

i want to parse the content and show it in a recyclerview.

everything is good, but when i run it this error happens:

Value Array of type java.lang.String cannot be converted to JSONObject

inside my json file there is a word i want to remove it by the code (because i can't edit the resource)

Array <------------//i want to remove this word
{"articles" : [{"name":"fiat uno ",
"description":"fiat uno 
model2002",
"price":"32000dh",
"seen":"5",
"username":"Kech",
"picture":"http:\/\/www.wach.ma\/files\/pictures\/1508420901.png",
"city":"Marrakech",
"phone":"0666353083"}]}

my activity where i want to do that:

public class Details extends AppCompatActivity {
private static final String URL_DATA = "http://wach.ma/mobile/item.php?
id=15236";

private RecyclerView recyclerView2;
private RecyclerView.Adapter adapter;
private List<ListDetail> listDetails;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    recyclerView2= (RecyclerView) findViewById(R.id.recyclerview2);
    recyclerView2.setHasFixedSize(true);
    recyclerView2.setLayoutManager(new LinearLayoutManager(this));
    listDetails=new ArrayList<>();
    loadRecyclerViewData();
}
private void loadRecyclerViewData(){
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Chargement...");
    progressDialog.show();
    StringRequest stringRequest=new StringRequest(Request.Method.GET, 
URL_DATA, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressDialog.dismiss();
            try {
                JSONObject jsonObject2=new JSONObject(response);
                JSONArray array=jsonObject2.getJSONArray("articles");
                for(int i = 0; i<array.length();i++) {
                    JSONObject o = array.getJSONObject(i);
                    ListDetail item = new ListDetail(
                            o.getString("picture"),
                            o.getString("name"),
                            o.getString("city"),
                            o.getString("username"),
                            o.getString("price"),
                            o.getString("phone"),
                            o.getString("description")
                    );
                    listDetails.add(item);
                }
                adapter = new DetailAdapter(listDetails, 
getApplicationContext());
                recyclerView2.setAdapter(adapter);

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

        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), 
error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    RequestQueue requestQueue= Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
}

i'm sorry for my bad english. thanks in advance

4 Answers4

0

You can use the string replace method as and replace the word array and replace it with an empty String and hence use, Check if your method logic looks like this code you also have alternatives String methods like "replace(),substring(), or finding the index of "{" but always trim() first (remove spaces at the beginning and end of your String):

try {
            String new_string=response.trim().substring(5);
            JSONObject jsonObjectRoot=new JSONObject(new_string);
            JSONArray jsonArrayArticles=jsonObjectRoot.getJSONArray("articles");
            for(int i = 0; i<array.length();i++) {
                JSONObject o = array.getJSONObject(i);
                ListDetail item = new ListDetail(  
       ....// Your more code here
Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • where can i use it please? – Hamiprogrammer Nov 03 '17 at 11:50
  • On response check I have edited the question remove the "Array" word will remove it in response! let me add more code – Xenolion Nov 03 '17 at 11:52
  • Have you see that part in your code @Hamiprogrammer?? – Xenolion Nov 03 '17 at 11:54
  • `{ .... "username" : "Array" ... }` and you are doomed ... please, stop posting replace as an answer because it's wrong!!! – Selvin Nov 03 '17 at 11:55
  • Why? is it wrong? check his response here in his question http://wach.ma/mobile/item.php?id=15236 the string has to miss the Array thing! – Xenolion Nov 03 '17 at 11:57
  • @Xenolion it's not worked and JSONObject jsonObject=new JSONObject(newString); jsonObject variable is never used – Hamiprogrammer Nov 03 '17 at 11:57
  • @Selvin please reconsider and remove your downvote check his link http://wach.ma/mobile/item.php?id=15236 the response should have "Array" word removed! – Xenolion Nov 03 '17 at 11:59
  • Lol ... and any "Array" in the response ... so fx http://wach.ma/mobile/item.php?id=15237 may can contains "Array" in some field and will be removed ... – Selvin Nov 03 '17 at 12:01
  • @Selvin please how can i fix that? put your reponse as an answer to vote it – Hamiprogrammer Nov 03 '17 at 12:02
  • 1
    @Hamiprogrammer check my ans – Ratilal Chopda Nov 03 '17 at 12:08
  • I have updated my answer @Hamiprogrammer It will work. If not send your logs so as to trace the bug! – Xenolion Nov 03 '17 at 12:19
  • Visited your profile and I start laughing when you said you like downvoting too much @Selvin . Why? Can we chat? – Xenolion Nov 03 '17 at 12:33
  • Okay @Hamiprogrammer NOW you will have to put a tick in my answer and accept it as it has solved your problem. You are the only one in the world who can do that because you asked the question!! In the left of the MY answer you fill the option to put a tick (stack_overflow_rule). **Happy Coding!**. – Xenolion Nov 03 '17 at 14:54
  • @Selvin as you can see above. The user who asked the question is happy with my answer (although he does not know how to accept it) please reconsider your downvote in my answer for future users! – Xenolion Nov 04 '17 at 04:31
  • Oooh I have seen it, Thank you and **Happy Coding!** @Hamiprogrammer – Xenolion Nov 04 '17 at 10:25
0

Try this use substring()

String newJSON=response.substring(5);
JSONObject jsonObject2=new JSONObject(newJSON);
JSONArray array=jsonObject2.getJSONArray("articles");
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
  • `{ .... "username" : "Array" ... }` and you are doomed ... please, stop posting replace as an answer because it's wrong!!! – Selvin Nov 03 '17 at 11:54
  • @Selvin didn't get u' – Ratilal Chopda Nov 03 '17 at 11:55
  • 1
    *didn't get u'* Yeah, I know ... that's why you don't now why it's a wrong answer ... replace will replace every "Array" occurrence which is wrong – Selvin Nov 03 '17 at 11:56
  • I've already wrote .... What if "username" will be "Array" (or link will be contains Array or whatever? Just think: after your code it will be ""(or wrong link) – Selvin Nov 03 '17 at 12:04
  • *`response.substring(0,5);`* Still wrong ... now newJSON is "Array" and nothing more ... but you are on good track – Selvin Nov 03 '17 at 12:09
  • This is the most funny person I have ever met in stackOverflow. Although he know the answer. He does not answer a question it but waiting and downvoting others seriously. He also downvoted mine too. I like him! @Selvin – Xenolion Nov 03 '17 at 13:01
0

Problem is due to invalid json string.

always truncate whitespace and other text before and after curly brackets of json string...

This way you can do this.

 String new_string = response.trim().substring(response.indexOf('{'), 
 response.lastIndexOf('}'));
darkknight
  • 5
  • 2
  • 8
-1

You cannot remove the invalid json variable from your code. Try to parse json in a valid format

moody_user
  • 41
  • 1
  • 4