在用Volley库解析JSON之后,我并没有任何错误,但结果并没有显示出来

2024-09-28 22:42:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Volley库通过Android访问json数据,以及我使用的web服务器的后端Python。给你是我的服务器端代码:

def showAndroid(request):
    users=User.objects.all()
    data = serializers.serialize('json', users, fields=('nom', 'prenom', 'postnom','mail'))
    return HttpResponse(data, content_type='application/json')

当我访问URL时,我得到以下结果:

enter image description here

另请参见我的android代码:

package com.example.lislis.mesexercices;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class UserJson extends AppCompatActivity {
EditText nom,prenom,postnom,email;
Button ajouter,voir;
    TextView list;
RequestQueue requestQueue;
String insertUrl="http://192.168.1.14:8000/webservices/insert-users/";
    String showUrl="http://192.168.1.14:8000/webservices/afficher-users/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_json);
        nom=(EditText)findViewById(R.id.nom);
        prenom=(EditText)findViewById(R.id.prenom);
        postnom=(EditText)findViewById(R.id.postnom);
        email=(EditText)findViewById(R.id.email);
        ajouter=(Button) findViewById(R.id.ajouter_user);
        voir=(Button)findViewById(R.id.afficher_users);
        list=(TextView)findViewById(R.id.list_users);

        requestQueue= Volley.newRequestQueue(this);

        voir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, showUrl, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray users=response.getJSONArray("data");
                            for(int i=0;i<users.length();i++)
                            {
                                JSONObject user=users.getJSONObject(i);
                                String fname=user.getString("prenom");
                                String lname=user.getString("nom");
                                String mname=user.getString("postnom");
                                String mail=user.getString("mail");
                                list.append(fname+" "+lname+" "+mname+" "+mail+"\n");
                                System.out.println("Tototo\n"+fname+" "+lname+" "+mname+" "+mail+"\n");
                            }
                            list.append("===\n=");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

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

                    }
                });
                requestQueue.add(jsonObjectRequest);
            }
        });
    }
}

但当我单击按钮显示JSON数据时,在我的django服务器监视器中,我可以看到一个状态代码为200的成功GET请求: enter image description here

但是为了测试服务器是否一切正常,我尝试用Ajax请求创建一个HTML文件,并将该文件复制到同一个WLAN中的另一台计算机上,我访问json时没有任何限制问题。是吗我的源代码有问题吗?你知道吗


Tags: importcomidjsonstringmailnomusers
2条回答

从您的响应来看,您应该从初始JSONObject获取一个JSONArray,而不需要访问该数组的键值“data”。所以,很可能抛出了一个JSONException,因为您没有正确地解析它。你知道吗

要检查这一点,您可以查看在抛出异常的地方添加Log.d语句是否打印出堆栈跟踪。你知道吗

如果您可以访问后端代码,那么可以很容易地通过添加一个“data”字段来获取数组来解决这个问题,否则您将不得不以不同的方式解析JSON。你知道吗

JSONArray users=response.getJSONArray("data"); 复发 JSONArray用户=响应.getJSONArray(“字段”)

相关问题 更多 >