7 Replies - 549 Views - Last Post: 21 July 2019 - 08:59 PM

#1 SoftwareAccount   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-July 19

Error in Android app

Posted 17 July 2019 - 08:31 AM

I have the below code which returns the FirstName, LastName and Class of student stored in database in json format. But when I run the app, data is not displayed
package com.example.anitaa.student;

import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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.JsonRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

public class MainActivity extends AppCompatActivity {
    TextView FirstName;
    TextView LastName;
    TextView Class;
    Button b1;
    String url1="http://192.168.1.3/student/web/studentrecords";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirstName=(TextView) findViewById(R.id.firstname1);
        LastName=(TextView) findViewById(R.id.lastname1);
        Class=(TextView) findViewById(R.id.class1);
        b1=(Button) findViewById(R.id.btn3);
        b1.setonclickListener(new View.onclickListener() {
            @Override
            public void onclick(View v) {
                JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url1,  null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            FirstName.setText( response.getString ( "FirstName").toString ());
                            LastName.setText(response.getString("LastName").toString ());
                            Class.setText(response.getString("Class").toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onerrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, error.getMessage(),Toast.LENGTH_SHORT).show();
                        error.printStackTrace();

                    }
                });
                MySingleton.getInstance(MainActivity.this).addToRequestque(jsonObjectRequest);
            }

        });
    }
}



Exception is displayed as

org.json.JSONExcetion: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject



What should I do?

Is This A Good Question/Topic? 0
  • +

Replies To: Error in Android app

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15171
  • View blog
  • Posts: 60,720
  • Joined: 12-June 08

Re: Error in Android app

Posted 17 July 2019 - 08:40 AM

I would look at line 41 and verify you can connect to something and are indeed getting data back.
Was This Post Helpful? 0
  • +
  • -

#3 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 50
  • View blog
  • Posts: 693
  • Joined: 23-September 15

Re: Error in Android app

Posted 17 July 2019 - 08:53 AM

Can you post your json response here by using some API testing tool? Eg: postman

This post has been edited by wseng92: 17 July 2019 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Error in Android app

Posted 17 July 2019 - 09:26 AM

What source line number is shown in the full text of the error message?
Was This Post Helpful? 0
  • +
  • -

#5 SoftwareAccount   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-July 19

Re: Error in Android app

Posted 18 July 2019 - 07:54 AM

View Postwseng92, on 17 July 2019 - 08:53 AM, said:

Can you post your json response here by using some API testing tool? Eg: postman

[
    {
        "Id": 1,
        "FirstName": "Mark",
        "LastName": "Anderson",
        "Class": "10th"
    }
]



Using postman I can get the result very well. But in android app there is problem.
Was This Post Helpful? 0
  • +
  • -

#6 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 50
  • View blog
  • Posts: 693
  • Joined: 23-September 15

Re: Error in Android app

Posted 18 July 2019 - 10:30 AM

Use JsonArrayRequest instead of JsonObjectRequest
JsonArrayRequest request = new JsonArrayRequest(url,
   new Response.Listener<JSONArray>() {
       @Override
       public void onResponse(JSONArray response) {
           try {
               // response is the JSONArray that you need to iterate
               for (int i = 0; i < response.length(); i++) {
                  JSONObject obj = response.getJSONObject(i);
                 String name = obj.getString ("FirstName");
                 String lastName = obj. getString ("LastName");
                            ........
               }
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   }, new Response.ErrorListener() {
     @Override
     public void onerrorResponse(VolleyError error) {
       error.printStackTrace();
     }
});

Not sure will this work,but it worth to try.

This post has been edited by wseng92: 18 July 2019 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 SoftwareAccount   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-July 19

Re: Error in Android app

Posted 20 July 2019 - 04:43 AM

View Postwseng92, on 18 July 2019 - 10:30 AM, said:

Use JsonArrayRequest instead of JsonObjectRequest
JsonArrayRequest request = new JsonArrayRequest(url,
   new Response.Listener<JSONArray>() {
       @Override
       public void onResponse(JSONArray response) {
           try {
               // response is the JSONArray that you need to iterate
               for (int i = 0; i < response.length(); i++) {
                  JSONObject obj = response.getJSONObject(i);
                 String name = obj.getString ("FirstName");
                 String lastName = obj. getString ("LastName");
                            ........
               }
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   }, new Response.ErrorListener() {
     @Override
     public void onerrorResponse(VolleyError error) {
       error.printStackTrace();
     }
});

Not sure will this work,but it worth to try.


The code is working. Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 50
  • View blog
  • Posts: 693
  • Joined: 23-September 15

Re: Error in Android app

Posted 21 July 2019 - 08:59 PM

I'm glad I could helped.

This post has been edited by wseng92: 21 July 2019 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1