Why am I getting NullPointerException error in this Volley onResponse? - android

In my app I am posting the user's phone number and getting a JSON Array as response, of the form [{"category" : "category", "name" : "name", "phone" : "phone", "comment" : "comment"}, etc etc ..... ]
I am trying to format this JSON Array response in my ListView but I am getting this error and my app shuts down. Help would be appreciated.
java.lang.NullPointerException
at appname.onResponse(ListView.java:130)
at appname.onResponse(ListView.java:83)
Below is my code :
StringRequest stringRequest = new StringRequest(Request.Method.POST, SelectUserReviews_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(ListView.this, response, Toast.LENGTH_LONG).show();
JsonArrayRequest movieReq = new JsonArrayRequest(SelectUserReviews_URL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Review review = new Review();
review.setCategory(obj.getString("category"));
review.setName(obj.getString("name"));
review.setPhone(obj.getString("phone"));
review.setComment(obj.getString("comment"));
reviewList.add(review);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error geting user info: " + error.getMessage());
hidePDialog();
}
});
// Adding request for getting user info to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error getting user: " + error.getMessage());
hidePDialog();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_PHONENUMBER_USER, phoneNoofUser);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
Line 130 is :
AppController.getInstance().addToRequestQueue(movieReq);
Line 83 is :
new Response.Listener<String>() {

Related

Volley What Request Type to use for the format

I'm New to android Volley. I accessing a API server and its response is JSON.
The response JSON is like this\
{
status:true,
data : [{
id:1,
name:'name 1',
},
{
id:2,
name:'name 2 ',
}]
}
I tried using JsonObjectRequest & JsonArrayRequest in Volley. Both throws error. What is the Right request type to use and How to parse it?
You will need to use a JsonObjectRequest for this, if it is a post request use the following
try{
JSONObject jsonBody = new JSONObject("add json body to post");
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(LookUp.url, jsonBody,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// pDialog.cancel();
try{
if(response.getString("status").equals("true")){
JSONArray jsonArray=response.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name=jsonObject.getString("name");
}
}
}
catch (JSONException error){
Log.e("TAGJE", error.getMessage());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAGE", error.getMessage(), error);
}
}){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
return params;
}
};
mQueue.add(jsonObjectRequest);
}
catch (JSONException ex){
ex.printStackTrace();
}
and for a get request follow this
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try{
if(response.getString("status").equals("true")){
JSONArray jsonArray=response.getJSONArray("data");
for(int i=0;i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name=jsonObject.getString("name");
}
}
}
catch (JSONException error){
Log.e("TAGJE", error.getMessage());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});

Listview not inflating with json

i have to inflate listview with the Json data with parameters . But it gives error 2.onErrorResponse: MainActivity . I checked multiple times with String Request and Json Request but none of them is working... Please Help!!!
JsonArrayRequest movieReq = new JsonArrayRequest(Request.Method.POST,url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
title_array.add(obj.getString("amount").toString());
notice_array.add(obj.getString("payment_id").toString());
title1_array.add(obj.getString("invoice_id").toString());
notice1_array.add(obj.getString("payment_date").toString());
notice2_array.add(obj.getString("movieName").toString());
notice3_array.add(obj.getString("source").toString());
adapter = new CustomBaseAdapter(Transaction.this, title_array, notice_array,title1_array,notice1_array,notice2_array,notice3_array);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
}){
#Override
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("user_id", user_id);
params.put("user_type", User_type);
return params;
}
};
// Adding request to request queue
requestQueue.add(movieReq);

How to Upload Image file Into Server along with my string params Using Volley

I am Trying to upload Images using volley library, I dont know How exactly.
I have referred previously asked questions, but nothing seems to satisfy.
Here is what i needed to acheive :
Below is the Code I tried to use :
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("**********", " Response Received is " + response);
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("**********", "STRING TO JSON CONVERSION DONE , IT IS " + jsonObject.toString());
ParseReqOtp parseReqOtp = new ParseReqOtp();
parseReqOtp.parseImageupload(jsonObject);
} catch (Exception e) {
Log.d("**********", "ERROR IN STRING TO JSON CONVERSION " + e.toString());
}
Log.d("**********", "FETCHING IN VOLLEY REQ" + response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(AddPic.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected HashMap<String,String> getParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("id", BaseActivity.getBaseActivity().getEstablishment_id());
params.put("type", "photos");
params.put("example_file",file.toString());
}
};
private void uploadImageWithStringParamsToServer() {
SimpleMultiPartRequest smr = new SimpleMultiPartRequest(Request.Method.POST, "http://139.59.16.103/addImage",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("Response", response);
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
smr.addStringParam("id", "821470");
smr.addStringParam("type", "photos");
smr.addFile("example_file", "2014-11-17 17.08.33-2.jpg");
smr.setFixedStreamingMode(true);
smr.setOnProgressListener(this);
RequestQueue mRequestQueue = Volley.newRequestQueue(this);
mRequestQueue.add(smr);
mRequestQueue.start();
}

Unable to make jsonarrayrequest with volley

Im trying to make a post request with volley.
The request parameter is a json array .
Following is my request parameter
[
"Type",
{
"User": "email",
"password": "dimmer",
}
]
I have a method frameJsonArray that frames the above json and im making post request as follows,
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
Im getting error in the above line of code where im making the request. How can I get this sorted?
Following is my error log
Error:(162, 46) error: constructor JsonArrayRequest in class JsonArrayRequest cannot be applied to given types;
required: String,Listener<JSONArray>,ErrorListener
found: int,String,JSONArray,<anonymous Listener<JSONObject>>,<anonymous ErrorListener>
reason: actual and formal argument lists differ in length
Following is my frameJsonArrayMethod
public JSONArray frameJsonArray() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("login_type", "Android");
jsonObject.put("username", email);
jsonObject.put("password", password);
jsonObject.put("short_name", null);
jsonObject.put("ip","123.421.12.21");
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonArray = new JSONArray();
jsonArray.put("Login");
jsonArray.put(jsonObject);
Log.d(TAG, "he;ll " + jsonArray.toString());
Toast.makeText(getApplicationContext(), jsonArray.toString(), Toast.LENGTH_SHORT).show();
return jsonArray;
}
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
Source : http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
once check your JSON , wrong format , you missed double quote.
[
"Type",
{
"User": "email /** you missed double quote here **/,
"password": "dimmer",
}
]
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.POST, Constants.requestUrl, frameJsonArray(),
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
hideDialog();
error.printStackTrace();
}
}
);
Change JSONObject to JSONArray in onResponse()
EDIT The error coming because there is only one constructor in JsonArrayRequest i.e.
public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener){}
source https://android.googlesource.com/platform/frameworks/volley/+/e7cdf98078bc94a2e430d9edef7e9b01250765ac/src/com/android/volley/toolbox/JsonArrayRequest.java
you are using some constructor with 5 arguments.

Volley sends null parameters to server

I'm trying to send JSON parameter but server is receiving them as nulls values,
i tried to request it from Postman and it works perfect, i don't know what the problem is with volley i followed the instructions here but it didn't make sense
here is my code
String url = "http://10.10.10.166:8080/SystemManagement/api/Profile/Login";
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("user_id","Test user name");
jsonObject.put("user_password","test password");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(jsonObject.toString());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
});
//add request to queue
queue.add(jsonObjectRequest);
I faced same problem, solved by overriding the getParams() method
Here is my login request using Volley.
private void loginRequeset() {
showpDialog();
StringRequest jsonObjReq = new StringRequest(Request.Method.POST, Constants.LOGIN_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
})
{
#Override
protected Map<String, String> getParams() {
Map<String, String> signup_param = new HashMap<String, String>();
signup_param.put(Constants.USERNAME, _emailText.getText().toString().trim());
signup_param.put(Constants.PASSWORD, _passwordText.getText().toString().trim());
return signup_param;
}
};
// Adding request to request queue
queue.getInstance().addToRequestQueue(jsonObjReq);
}

Categories

Resources