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?