Results 1 to 12 of 12
  1. #1
    cdubs is offline Member
    Join Date
    Apr 2016
    Posts
    9
    Rep Power
    0

    Exclamation Cannot receive json from server

    I am trying to receive a username from a database and all the information that goes with that username. I am using a session in the php and applying the session ID in my get request in android. When I test this in a browser I get the correct json array from the server. However in my application the request is null. I am not sure why. Like I said, it is getting the information perfectly when entered in the browser. I know for a fact I am applying the correct session id as well. Here is the java method:

    Java Code:
    private void getUserData() {
    
            SharedPreferences sharedPreferences = getSharedPreferences(Config.sharedPref, Context.MODE_PRIVATE);
            String sessionId = sharedPreferences.getString(Config.SID, "SessionID");
    
            StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.SERVER_ADDRESS + 
    "GetUserData.php?PHPSESSID=" + sessionId,
    
                    new Response.Listener<String>() {
    
                        @Override
                        public void onResponse(String response) {
    
                            JSONObject jsonObject = null;
                            try {
    
                                //json string to jsonobject
                                jsonObject = new JSONObject(response);
    
                                //get json sstring created in php and store to JSON Array
                                result = jsonObject.getJSONArray(Config.json_array);
    
                                //get username from json array
                                getUserInfo(result);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
    
                        }
    
                    });
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Cannot receive json from server

    Stick some debug code in there for the onError part. You might be ignoring an error state.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    cdubs is offline Member
    Join Date
    Apr 2016
    Posts
    9
    Rep Power
    0

    Default Re: Cannot receive json from server

    So add something like this?
    Java Code:
    public void onErrorResponse(VolleyError error) {
    
                            VolleyLog.e("Error: " + error.getMessage());
    
                        }
    The debugging skips past everything in this method and goes straight tho the requestqueue. If I hover over error it says cannot find local variable.
    Cannot receive json from server-erimg.jpg

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Cannot receive json from server

    I didn't mean actually stick a debug point, but just log anything coming in there into the logs.
    But if it never enters there then it means that there isn't a hidden error.

    When you say the request is null, which thing is null?

    Also, how are you running this?
    Where is it in relation to the browser you used?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    cdubs is offline Member
    Join Date
    Apr 2016
    Posts
    9
    Rep Power
    0

    Default Re: Cannot receive json from server

    I'm really not sure. It's not even entering into the method like I said. Just skips straight to:
    Java Code:
    RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
    When I debug and look at the onResponse, it says its null. In the browser I type in the getuserdata.php and attach the session id. I get back what I am suppose to.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Cannot receive json from server

    It says what is null?
    onResponse can't be null as it's a method.
    I can't tell what you think is null?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    cdubs is offline Member
    Join Date
    Apr 2016
    Posts
    9
    Rep Power
    0

    Default Re: Cannot receive json from server

    I believe the actual response is null. The string request is getting "[ ]".

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Cannot receive json from server

    Then log the response string, putting some obvious character at the beginning and end:
    "#" + response + "#"

    That way you can confirm it is blank.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    cdubs is offline Member
    Join Date
    Apr 2016
    Posts
    9
    Rep Power
    0

    Default Re: Cannot receive json from server

    Oh wow. The response is correct. Its this line that is screwing up.

    result = jsonObject.getJSONArray(Config.json_array);

    result is the one getting null.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Cannot receive json from server

    So what value is Config.json_array?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    cdubs is offline Member
    Join Date
    Apr 2016
    Posts
    9
    Rep Power
    0

    Default Re: Cannot receive json from server

    Config.json_array = "result". Which is what I want. In my server side php code I have: echo json_encode(array('result'=>$result)); For some reason it's not assigning the response to the json array result.

  12. #12
    cdubs is offline Member
    Join Date
    Apr 2016
    Posts
    9
    Rep Power
    0

    Default Re: Cannot receive json from server

    I figured it out! I was using config.json_array for another encode to populate a dropdown. I guess it didn't like that I was using it twice. I made a new variable and changed the jsonencode and called the new variable. Thank you so much for the help.

Similar Threads

  1. Replies: 0
    Last Post: 07-16-2012, 07:44 PM
  2. Replies: 0
    Last Post: 07-15-2012, 07:57 AM
  3. Client cannot receive inputstream from server
    By chyrl in forum Networking
    Replies: 2
    Last Post: 08-29-2010, 05:13 PM
  4. How to receive byte array from Server?
    By k80sg in forum New To Java
    Replies: 0
    Last Post: 03-18-2010, 08:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •