Java Android从Okhttp onResponse函数提取字符串

Java Android从Okhttp onResponse函数提取字符串,java,android,string,handler,okhttp,Java,Android,String,Handler,Okhttp,我想使用androidstudio从OkHttp3中的onResponse函数中获取字符串值,它是 最终字符串title\u name=jsonarray\u news\u extract\u Data(jsonStr,索引) 在下面的代码中。 我尝试在参数中插入字符串变量“a”,但它说 变量是在内部类中访问的。需要宣布为最终决定 所以我宣布为最终结果,然后它说 无法为最终变量赋值 我试着把 a = title_name; 删除处理程序方法后,仍不起作用 如果你有任何想法,请帮助我。 多谢各位

我想使用androidstudio从OkHttp3中的onResponse函数中获取字符串值,它是

最终字符串title\u name=jsonarray\u news\u extract\u Data(jsonStr,索引)

在下面的代码中。 我尝试在参数中插入字符串变量“a”,但它说

变量是在内部类中访问的。需要宣布为最终决定

所以我宣布为最终结果,然后它说

无法为最终变量赋值

我试着把

a = title_name;
删除处理程序方法后,仍不起作用

如果你有任何想法,请帮助我。 多谢各位

public void news_From_API_Title(String url_news, final int index, final TextView textView, String a){

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(url_news)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {}

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            final String jsonStr = Objects.requireNonNull(response.body()).string();
            final String title_name = jsonarray_news_extract_Data(jsonStr, index);

            Handler mainHandler = new Handler(Looper.getMainLooper());
            mainHandler.post(new Runnable() {
                @Override
                public void run() {

                    a = title_name;
                }
            });
        }
    });

}

private String jsonarray_news_extract_Data(String jsonString_News, int index){
    try {
        JSONObject jsonObject = new JSONObject(jsonString_News);
        JSONArray jsonArray = jsonObject.getJSONArray("Data");
        return jsonArray.getJSONObject(index).getString("title");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

您可以像这样使用自定义的
界面

interface ApiCallback{
    void onOkHttpResponse(String data);
    void onOkHttpFailure(Exception exception);
}
然后,将
字符串
传递给实际执行调用的方法,而不是
字符串
,而是
ApiCallback
的实例:

public void news_From_API_Title(String url_news, final int index, final TextView textView, ApiCallback callback){

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(url_news)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            callback.onOkHttpFailure(e);
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            final String jsonStr = Objects.requireNonNull(response.body()).string();
            final String title_name = jsonarray_news_extract_Data(jsonStr, index);

            Handler mainHandler = new Handler(Looper.getMainLooper());
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    callback.onOkHttpResponse(title_name);
                }
            });
        }
    });

}
现在,您可以使用
ApiCallback
的实现,如下所示:

String url_news = "https://some.web.address/news";
int index = 7;
TextView textView;
news_From_API_Title(url_news, index, textView, new apiCallback(){
    @Override
    public void onOkHttpResponse(String data){
        // do something with data here
        Log.d("TEST", "data = " + data);
    }

    @Override
    onOkHttpFailure(Exception exception){
        // exception handling
    }
});

请注意,如果不将
TextView
作为参数传递到方法中,而是在
onOkHttpResponse()中设置文本,则从体系结构的角度来看可能会更好:


您可以像这样使用自定义的
界面

interface ApiCallback{
    void onOkHttpResponse(String data);
    void onOkHttpFailure(Exception exception);
}
然后,将
字符串
传递给实际执行调用的方法,而不是
字符串
,而是
ApiCallback
的实例:

public void news_From_API_Title(String url_news, final int index, final TextView textView, ApiCallback callback){

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(url_news)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            callback.onOkHttpFailure(e);
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            final String jsonStr = Objects.requireNonNull(response.body()).string();
            final String title_name = jsonarray_news_extract_Data(jsonStr, index);

            Handler mainHandler = new Handler(Looper.getMainLooper());
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    callback.onOkHttpResponse(title_name);
                }
            });
        }
    });

}
现在,您可以使用
ApiCallback
的实现,如下所示:

String url_news = "https://some.web.address/news";
int index = 7;
TextView textView;
news_From_API_Title(url_news, index, textView, new apiCallback(){
    @Override
    public void onOkHttpResponse(String data){
        // do something with data here
        Log.d("TEST", "data = " + data);
    }

    @Override
    onOkHttpFailure(Exception exception){
        // exception handling
    }
});

请注意,如果不将
TextView
作为参数传递到方法中,而是在
onOkHttpResponse()中设置文本,则从体系结构的角度来看可能会更好:


您可以使用大小为1的字符串数组,并在遇到这种情况时使用该数组存储值。更好的方法是使用数据模型并为模型设置setter。数组方法->最终字符串[]a=新字符串[1],然后是[0]=标题\u名称您可以使用大小为1的字符串数组,并在遇到这种情况时使用该数组存储值。更好的方法是使用数据模型并为模型设置setter。数组方法->最终字符串[]a=新字符串[1],然后是[0]=标题\u名称非常感谢您的回答。我想将字符串值传递到另一个片段中,使用接口可以完美地工作。同时也感谢您对Textview的建议。在我发布之前,我忘了删除。不管怎样,你的答案很好!非常感谢你!非常感谢你的回答。我想将字符串值传递到另一个片段中,使用接口可以完美地工作。同时也感谢您对Textview的建议。在我发布之前,我忘了删除。不管怎样,你的答案很好!非常感谢你!