android - 检索 Json 的字符串数据
问题描述
我是 Json 的新手。我无法理解如何解析这个 Json。我正在使用 volley 并在 recyclerview 中检索数据。帮助我检索此数据。
这是 json 外观:
[{
"id":"ahiru-no-sora-dub-episode-1"
},{
"id":"ahiru-no-sora-dub-episode-2"
},{
"id":"ahiru-no-sora-dub-episode-3"
},{"id":"ahiru-no-sora-dub-episode-4"
},{"id":"ahiru-no-sora-dub-episode-5"
},{"id":"ahiru-no-sora-dub-episode-6"
},{"id":"ahiru-no-sora-dub-episode-7"
},{"id":"ahiru-no-sora-dub-episode-8"
},{"id":"ahiru-no-sora-dub-episode-9"
},{"id":"ahiru-no-sora-dub-episode-10"
},{"id":"ahiru-no-sora-dub-episode-11"
},{"id":"ahiru-no-sora-dub-episode-12"
},{"id":"ahiru-no-sora-dub-episode-13"
},{"id":"ahiru-no-sora-dub-episode-14"
},{"id":"ahiru-no-sora-dub-episode-15"
},{"id":"ahiru-no-sora-dub-episode-16"
},{"id":"ahiru-no-sora-dub-episode-17"
},{"id":"ahiru-no-sora-dub-episode-18"
},{"id":"ahiru-no-sora-dub-episode-19"
},{"id":"ahiru-no-sora-dub-episode-20"}]
这是我从 Activity 调用的方法:
public class OnGoingDetails extends AppCompatActivity
{
private RecyclerView mRecyclerView;
private EpisodeListAdapter mExampleAdapter;
private ArrayList<EpisodeListModel> mExampleList;
private RequestQueue mRequestQueue;
private ProgressDialog loadingPop;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ongoing_detail);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
loadingPop = new ProgressDialog(this);
loadingPop.setTitle("Loading...");
loadingPop.setMessage("Please wait retrieving data from server");
loadingPop.setCanceledOnTouchOutside(false);
loadingPop.show();
Bundle extra = getIntent().getExtras();
String url = extra.getString("ID");
//Toast.makeText(OnGoingDetails.this,url, Toast.LENGTH_SHORT).show();
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response)
{
try
{
Toast.makeText(OnGoingDetails.this,s, Toast.LENGTH_SHORT).show();
JSONObject jsonob = new JSONObject();
JSONObject jo;
for(int i=0;i<jsonArray.length();i++){
jo=jsonArray.getJSONObject(i);
String ids=jo.getString("id");
loadingPop.dismiss();
Toast.makeText(OnGoingDetails.this, "Data: "+ids, Toast.LENGTH_SHORT).show();
ClipboardManager clipboard = (ClipboardManager) OnGoingDetails.this.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label","data"+ids); //ids.toString());
clipboard.setPrimaryClip(clip);
//Toast.makeText(getActivity(), "Data Refreshed", Toast.LENGTH_SHORT).show();
mExampleList.add(new EpisodeListModel(ids));
loadingPop.dismiss();
}
mExampleAdapter = new EpisodeListAdapter(OnGoingDetails.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
//Toast.makeText(getActivity(), "Added Data", Toast.LENGTH_SHORT).show();
loadingPop.dismiss();
}
catch (JSONException e)
{
e.printStackTrace();
Toast.makeText(OnGoingDetails.this, "Catch" + e, Toast.LENGTH_LONG).show();
loadingPop.dismiss();
}}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
if (error instanceof NetworkError)
{
loadingPop.dismiss();
Toast.makeText(OnGoingDetails.this,
"Oops. Network error!",
Toast.LENGTH_LONG).show();
}
else if (error instanceof ServerError)
{
loadingPop.dismiss();
Toast.makeText(OnGoingDetails.this,
"Oops. Server error!",
Toast.LENGTH_LONG).show();
}
else if (error instanceof AuthFailureError)
{
loadingPop.dismiss();
Toast.makeText(OnGoingDetails.this,
"Oops. Authintication error!",
Toast.LENGTH_LONG).show();
}
else if (error instanceof ParseError)
{
loadingPop.dismiss();
Toast.makeText(OnGoingDetails.this,
"Oops. Parse error!",
Toast.LENGTH_LONG).show();
}
else if (error instanceof NoConnectionError)
{
loadingPop.dismiss();
Toast.makeText(OnGoingDetails.this,
"No Internet Connection!",
Toast.LENGTH_LONG).show();
}
else if (error instanceof TimeoutError)
{
Toast.makeText(OnGoingDetails.this,
"Oops. Timeout error!",
Toast.LENGTH_LONG).show();
loadingPop.dismiss();
}
}
}
);
request.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(request);
}
}
*注意:我正在使用 Volley Online api 并使用 Intent(Bundle) 从另一个活动中获取此 json *
解决方案
推荐阅读
- c - 无符号长和 uint32_t?
- javascript - ajax请求失败后javascript失败
- database - perl 无法连接数据库
- typescript - `not a module` - 接口分配错误
- swift - 如何正确地将数据从一个视图控制器的选定单元格传递到前一个视图控制器?
- ruby-on-rails-5 - TypeError(无法投射文件):Rails Carrierwave 文件上传
- java - Spring Cloud Contract 问题从本地存储库中检索存根
- javascript - 我们什么时候在javascript导入中使用'{}'?
- uwp - 在用于 UWP 应用程序的曲棍球应用程序中获取异常确切的行号
- python - 使用 pip 安装 mysqlclient 时出错,找不到 -lssl 的库