ali taki
ali taki

Reputation: 11

JSON response is incomplete but in browser is ok

When I send a request to server after check response , I find incomplete JSON response. I use Volley , How to solve it?

resonse is here :

{"mostviewedvideos":[{"id":"18253295","title":"\u06a9\u062c\u0627\u0628\u0631\u06cc\u0645 \u0642\u0633\u0645\u062a5 \/ \u0641\u0633\u062a \u0641\u0648\u062f \u062a\u0648\u0633\u06a9\u0627","username":"hormozgantv","userid":"3362417","visit_cnt":464,"uid":"JiE8T","isHidden":false,"process":"done","big_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18253295-9314-b__533446055.jpg","small_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18253295-9314__8805.jpg","profilePhoto":"https:\/\/www.aparat.com\/public\/public\/user_data\/profile_photo\/1121\/3362417-m.jpg","duration":448,"sdate":"21 \u0622\u0630\u0631 1398","create_date":"2019-12-12 13:06:59","sdate_timediff":168361,"frame":"https:\/\/www.aparat.com\/video\/video\/embed\/videohash\/JiE8T\/vt\/frame","official":"yes","autoplay":true,"video_date_status":"notset","360d":false,"deleteurl":""},{"id":"18261013","title":"\u0622\u0645\u0648\u0632\u0634 \u0631\u0648\u0627\u0646\u0634\u0646\u0627\u0633\u06cc: \u0628\u0647 \u062e\u0627\u0637\u0631 \u0639\u0634\u0642\u062a \u0627\u0632 \u062e\u0648\u062f\u062a \u0646\u06af\u0630\u0631","username":"halimeh_basefat","userid":"6317482","visit_cnt":788,"uid":"tSwQV","isHidden":false,"process":"done","big_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18261013-7821-b__699295836.jpg","small_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18261013-7821__7696.jpg","profilePhoto":"https:\/\/www.aparat.com\/public\/public\/user_data\/profile_photo\/2106\/6317482-m.jpg","duration":165,"sdate":"21 \u0622\u0630\u0631 1398","create_date":"2019-12-12 22:06:51","sdate_timediff":135969,"frame":"https:\/\/www.aparat.com\/video\/video\/embed\/videohash\/tSwQV\/vt\/frame","official":"yes","autoplay":true,"video_date_status":"notset","360d":false,"deleteurl":""},{"id":"18276419","title":"\u062a\u0627\u06cc\u0645 \u0644\u0650\u067e\u0633 \u06af\u0648\u0634\u062a \u0686\u0631\u062e \u0634\u062f\u0647 - Minced Meat TimeLapse","username":"meysam.ahwazi","userid":"2653435","visit_cnt":1138,"uid":"xiYy1","isHidden":false,"process":"done","big_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18276419-4985-b__189630358.jpg","small_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18276419-4985__9388.jpg","profilePhoto":"https:\/\/www.aparat.com\/public\/public\/user_data\/profile_photo\/885\/2653435-m.jpg","duration":314,"sdate":"22 \u0622\u0630\u0631 1398","create_date":"2019-12-13 19:55:27","sdate_timediff":57453,"frame":"https:\/\/www.aparat.com\/video\/video\/embed\/videohash\/xiYy1\/vt\/frame","official":"no","autoplay":true,"video_date_status":"notset","360d":false,"deleteurl":""},{"id":"18238407","title":"\u0641\u06cc\u0644\u0645 \u0633\u06cc\u0646\u0645\u0627\u06cc\u06cc 23 \u0646\u0641\u0631","username":"owjmedia","userid":"3275514","visit_cnt":1438,"uid":"VsfQ2","isHidden":false,"process":"done","big_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18238407-6210-b__612861992.jpg","small_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18238407-6210__3438.jpg","profilePhoto":"https:\/\/www.aparat.com\/public\/public\/user_data\/profile_photo\/1092\/3275514-m.jpg","duration":59,"sdate":"20 \u0622\u0630\u0631 1398","create_date":"2019-12-11 14:09:23","sdate_timediff":251017,"frame":"https:\/\/www.aparat.com\/video\/video\/embed\/videohash\/VsfQ2\/vt\/frame","official":"yes","autoplay":true,"video_date_status":"notset","360d":false,"deleteurl":""},{"id":"18273908","title":"\u062e\u0634\u0645 \u0645\u0631\u062f\u0645 \u06af\u06cc\u0644\u0627\u0646 \u0627\u0632 \u0633\u0631\u06cc\u0627\u0644 \u00ab\u0648\u0627\u0631\u0634\u00bb","username":"kelaket.com","userid":"750710","visit_cnt":31091,"uid":"azfQ3","isHidden":false,"process":"no_stat","big_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18273908-6376-b__412519292.jpg","small_poster":"https:\/\/static.cdn.asset.aparat.com\/avt\/18273908-6376__4969.jpg","profilePhoto":"https:\/\/www.aparat.com\/public\/public\/user_data\/profile_photo\/251\/750710-m.jpg","duration":132,"sdate":"22 \u0622\u0630\u0631 1398","create_date":"2019-12-13 17:

url for this site is :

https://www.aparat.com//etc/api/mostviewedvideos

in android get res (response) and convert to json :

 JSONObject object = new JSONObject(res);
            String aparat = object.getString("mostviewedvideos");

            JSONArray jsonArray = new JSONArray(aparat);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Items item = new Items();
                item.setTitle(jsonObject.getString("title"));
                item.setImageUrl(jsonObject.getString("small_poster"));
                item.setDate(jsonObject.getString("sdate"));
                item.setUsername(jsonObject.getString("username"));
                item.setAdvertising("none");
            }

but some object and some key not available in response.

Upvotes: 0

Views: 268

Answers (2)

Rubick
Rubick

Reputation: 306

I have logged your wanted data by using FastAndroidNetworking. Please see the sample code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getData();

}

private synchronized void getData() {
    OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
            .connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(15, TimeUnit.SECONDS)
            .writeTimeout(15, TimeUnit.SECONDS)
            .build();

    AndroidNetworking.initialize(this, okHttpClient);

    AndroidNetworking.get("https://www.aparat.com//etc/api/mostviewedvideos")
            .setPriority(Priority.HIGH)
            .addHeaders("Content-Type", "application/json")
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        String aparat = response.getString("mostviewedvideos");

                        JSONArray jsonArray = new JSONArray(aparat);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            String title = jsonObject.getString("title");
                            String small_poster = jsonObject.getString("small_poster");
                            String sdate = jsonObject.getString("sdate");
                            String username = jsonObject.getString("username");

                            Log.i("data", title + "\n" + small_poster + "\n" + sdate + "\n" + username + "\n");
                        }
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(ANError anError) {
                    errorHandler(anError);
                }
            });
}

public void errorHandler(ANError error) {
    if (error.getErrorCode() != 0) {

        Log.e("onError errorCode", "onError errorCode : " + error.getErrorCode());
        Log.e("onError errorBody", "onError errorBody : " + error.getErrorBody());
        Log.e("onError errorDetail", "onError errorDetail : " + error.getErrorDetail());

    } else {
        Log.e("onError errorDetail", "onError errorDetail : " + error.getErrorDetail());
        Log.e("onError errorBody", "onError errorBody : " + error.getResponse());

    }
}

}

Check this for documentation and steps to use it.

Upvotes: 2

Milad Bahmanabadi
Milad Bahmanabadi

Reputation: 968

Try this code, it works for me:

    JsonObjectRequest sendSign = new JsonObjectRequest(Request.Method.GET,
            "https://www.aparat.com//etc/api/mostviewedvideos", null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONObject object = new JSONObject(response.toString());
                String aparat = null;

                aparat = object.getString("mostviewedvideos");

                JSONArray jsonArray = new JSONArray(aparat);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Items item = new Items();
                    item.setTitle(jsonObject.getString("title"));
                    item.setImageUrl(jsonObject.getString("small_poster"));
                    item.setDate(jsonObject.getString("sdate"));
                    item.setUsername(jsonObject.getString("username"));
                    item.setAdvertising("none");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.e("response", response.toString());

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error",error.toString());

        }
    });

Upvotes: 0

Related Questions