0

Hi I am a beginner in Android and I am stuck in this problem.So this is what I am trying to do using a volley response object

private void parseJSONResponse(JSONObject response)
{
int a = jsonObject.getInt("xxx");  //get 1st parameter

String mid = jsonObject.getString("Movieid");  //get an id
String murl = genrate(mid);  //send id to genrate method which returns another url
String x=createmovieJSON(murl);//method to create JsonObjectRequest object using the new url and return string.


info.add(new information(a,x));  //pass int and string for generating a card layout
}

createmovieJSON method is as follows:

public String createmovieJSON(String strurl)
{
    final String[] str=(null);     //str[0] contains null
    RequestQueue requestQueue = Singelton.instantinate().getRequestqueue();                     

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, strurl,
            new Response.Listener<JSONObject>()                                                 
            {
                @Override
                public void onResponse(JSONObject response)
                {

                    str[0] = parseMovieResponse(response); //str[0] contains required string say'abc'
                    new Toastnotify(getApplicationContext(),str[0]);


                }
            },
            new Response.ErrorListener(){...                                                        
            });
    requestQueue.add(jsonObjectRequest);
    new Toastnotify(getApplicationContext(),str[0]);
    return(str[0]);    //str[0] contains null instead of abc
}

The 'final String[]' array is automatically created on correcting the error "Transform str into final one element array" in onResponse method.Why is it creating an array and not just make str final?(I tried doing that manually but it gives the same error).I know that it is a inner class and therefore I have to make it final but why array?

So how can I send the returned string in str[0] in onResponse back to the calling method in parseJSONResponse method?

Junior
  • 113
  • 1
  • 7
  • Pls read http://stackoverflow.com/questions/31602042/android-java-how-to-delay-return-in-a-method – BNK Dec 21 '15 at 06:09

1 Answers1

0

Change the code like this and try it: Create a CallBack interface, then send data to caller in via this CallBack

public void createmovieJSON(String strurl, CallBack callBack)
{

    RequestQueue requestQueue = Singelton.instantinate().getRequestqueue();                     

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, strurl,
            new Response.Listener<JSONObject>()                                                 
            {
                @Override
                public void onResponse(JSONObject response)
                {


                    new Toastnotify(getApplicationContext(),str[0]);
                    requestQueue.add(jsonObjectRequest);
                    new Toastnotify(getApplicationContext(),str[0]);

                    callBack.data(parseMovieResponse(response));
                }
            },
            new Response.ErrorListener(){...                                                        
            });

}

    interface CallBack {
        void data(String str);
    }
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • do you mean something like this: http://tinypic.com/r/2hqcu2v/9 ,it gives error variable json ObjectRequest might not have been initialized. – Junior Dec 21 '15 at 06:03
  • Yeah, because you get the data via network, then it is going to be execute in the another thread, so your method will return before the data received. So you need a callback when the data has been received, then you can get the data in the call back. – Bahramdun Adil Dec 21 '15 at 06:13