0

I have a mysql data base and a table in it, I encode it to json, when I run php file in browser i got something like this :

[
    {
        "product":"\u0645\u0627\u0633\u062a \u067e\u0631 \u0686\u0631\u0628 \u0633\u0648\u0646 \u06a9\u0627\u0644\u0647",
        "info":"\u0645\u0627\u0633\u062a \u0633\u0648\u0646 \u06f5 \u062f\u0631\u0635\u062f \u0686\u0631\u0628\u06cc \u06f1\u060c\u06f5 \u06a9\u06cc\u0644\u0648\u06cc\u06cc \u06a9\u0627\u0644\u0647",
        "price":"7\u060c800 \u062a\u0648\u0645\u0627\u0646",
        "date":null,
        "sale":"no sale",
        "image":"Image source",
        "buy":"Store"
    }
]

that i think its ok , but when I get it in my android code its like the picture linked below:

image

this is the code i used in android

StringRequest stringRequest = new StringRequest(Request.Method.GET, server_url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            try {
                JSONArray array = new JSONArray(response);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject product = array.getJSONObject(i);
                    productList.add(new Product(
                        product.getString("product"),
                        product.getString("info"),
                        product.getString("price"),
                        product.getString("date"),
                        product.getString("sale"),
                        product.getString("image"),
                        product.getString("buy")
                    ));
                }
                ProductAdapter adapter = new ProductAdapter(MainActivity.this, productList);
                recyclerView.setAdapter(adapter);
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },

    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }
);
Volley.newRequestQueue(this).add(stringRequest);

So what can I do? Thank you for your time.

  • Per @abestrad: Please provide additional information or details. Share your research on your problem; what have you found so far? Why didn't it work? Is the result not being parsed? Is this code throwing exceptions when parsing the result? Have you try libraries like Flexjson? – Hovercraft Full Of Eels Jan 27 '19 at 15:09
  • https://stackoverflow.com/questions/31912000/byethost-server-passing-html-values-checking-your-browser-with-json-string – Armin Irandoust Feb 02 '19 at 10:19

1 Answers1

0

I think you need to add an Accept header in your android request. The server is returning HTML because that is the default content-type. You should add a HTTP header "Accept: application/json".

vavasthi
  • 922
  • 5
  • 14