【问题标题】:Return a string from within a method used to create a jsonObjectRequest从用于创建 jsonObjectRequest 的方法中返回一个字符串
【发布时间】:2015-12-21 05:16:57
【问题描述】:

您好,我是 Android 的初学者,我遇到了这个问题。所以这就是我正在尝试使用凌空响应对象来做的事情

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 方法如下:

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
}

'final String[]' 数组是在更正 onResponse 方法中的错误“将 str 转换为最后一个元素数组”时自动创建的。为什么它创建一个数组而不只是使 str 最终?(我尝试手动执行此操作但它给出了同样的错误)。我知道它是一个内部类,因此我必须将它设为 final 但为什么是数组?

那么如何将 onResponse 中 str[0] 中返回的字符串发送回 parseJSONResponse 方法中的调用方法?

【问题讨论】:

标签: java android json android-volley


【解决方案1】:

像这样更改代码并尝试一下: 创建一个CallBackinterface,然后通过这个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);
    }

【讨论】:

  • 你的意思是这样的吗:tinypic.com/r/2hqcu2v/9,它给出了错误变量json ObjectRequest可能没有被初始化。
  • 是的,因为你是通过网络获取数据,然后它会在另一个线程中执行,所以你的方法会在接收到数据之前返回。所以需要在收到数据的时候进行回调,然后才能在回调中获取数据。
猜你喜欢
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
相关资源
最近更新 更多