简体   繁体   中英

After parsing JSON with Volley library i don't have any error but result is not displaying

I'm trying to access json data via Android using Volley Library,and for the backend of the webserver i've used python.Here is my server side code:

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

When I access the URL I obtain the following result:

在此处输入图片说明

See also my android code:

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

But when i click the button to display the JSON data,in my django server monitor, i can see a successful GET request with a status code 200: 在此处输入图片说明

But for testing if everything goes right in the server, I've tried to create a single HTML file with an Ajax request and have copied the file on another computer in my same WLAN , i access json without any problem.Is there any problem with my source code?

JSONArray users=response.getJSONArray("data"); relapse with JSONArray users=response.getJSONArray("fields");

It looks like from your response, that you should be getting a JSONArray back from the initial JSONObject without a key value of "data" for accessing that array. So, you're likely having a JSONException thrown, since you're not parsing it correctly.

To check this out, you could see if adding a Log.d statement where you exception is thrown prints out a stack trace.

If you have access to the backend code, you could pretty easily fix this by adding a "data" field for fetching the array, otherwise you'll have to go about parsing your JSON differently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM