【问题标题】:JSON response is incomplete but in browser is okJSON 响应不完整,但在浏览器中正常
【发布时间】:2019-12-14 08:12:39
【问题描述】:

当我在检查响应后向服务器发送请求时,我发现 JSON 响应不完整。我用 Volley 怎么解决?

共鸣在这里:

{"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:

这个网站的网址是:

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

在android中获取res(响应)并转换为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");
            }

但某些对象和某些键在响应中不可用。

【问题讨论】:

  • 请出示您的代码。
  • 添加android代码并编辑响应
  • 回复是否来自您的 logcat?
  • 我知道 android 中的 logcat 有大小限制,但无法从响应中找到分页键等
  • 您打算将其显示在列表中?

标签: java android json


【解决方案1】:

我已经使用FastAndroidNetworking 记录了您想要的数据。请看示例代码:

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

    }
}

}

查看this 获取文档和使用步骤。

【讨论】:

    【解决方案2】:

    试试这个代码,它对我有用:

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

    【讨论】:

      猜你喜欢
      • 2021-08-24
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      相关资源
      最近更新 更多