android - volley中的JSONRequest和StringRequest有什么区别

标签 android json http android-volley

我一直读到数据来自 json 格式的服务器,当我们想发送一些我们以 json 格式发送到服务器的数据时,数据以 json 格式传输,那么字符串请求来自哪里?我不知道我们是否也可以以字符串格式发布和获取数据,使用字符串和 json 请求有什么区别和用例?

谢谢!

最佳答案

StringRequest class will be used to fetch any kind of string data. The response can be json, xml, html,text.

 // Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

If you are expecting json object in the response, you should use JsonObjectRequest .

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            URL, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

关于android - volley中的JSONRequest和StringRequest有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45543544/

相关文章:

java - 不确定这个 Java 代码示例中发生了什么

javascript - 按 Angular.js 中的公共(public)对象属性过滤数组?

javascript - Highcharts 服务器端图像生成,如何在回调参数中设置图例 labelFormatter

json - Visual Studio Code侧边栏选择颜色

rest - 当在 URL 和正文中使用 id 进行 PUT 操作时,哪一个获胜?

android - 如何以编程方式启动应用程序详细信息?

android - 在自定义 Runnable 中使用 Retrofit 执行请求

android - 确保安装时使用的Node.js版本与运行时使用的版本匹配

c++ - libcurl 如何更改编码 url 行为

性能:绝对 URL 与相对 URL