Android的Fragment如何从okhttp获取数据?

本文介绍了在Android开发中如何使用OkHttp库在Fragment中进行GET和POST请求,通过创建一个Singleton工具类封装了OkHttp的请求方法,并提供了一个HttpResponseCallBack接口处理响应数据。遇到此类问题的开发者可以通过查看提供的项目代码来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开发某个Android项目:DateListThingsAnalyse-Android,其中用到了Fragment来展示数据,获取数据一直被卡住了。最后在StackOverflow找到了相关解决办法。建议还是去clone一下我的项目查看实际代码去体会用法。

okhttp工具类

public class Singleton {
    private static Singleton INSTANCE = null;
    private Singleton() {};

    OkHttpClient client = new OkHttpClient();
    public static final MediaType JSON
            = MediaType.get("application/json; charset=utf-8");

    public static Singleton getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Singleton();
        }
        return(INSTANCE);
    }

    public void doGetRequest(String url, final HttpResponseCallBack responseCallBack) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String strResponse = response.body().string();
                Log.v("getStream-strResponse",strResponse);
                try {
                    responseCallBack.getResponse(strResponse);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public void doPostRequest(String url,String json, final HttpResponseCallBack responseCallBack) throws IOException {
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();


        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String strResponse = response.body().string();
                Log.v("getStream-strResponse",strResponse);
                try {
                    responseCallBack.getResponse(strResponse);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

接口

package info.emperinter.DateListThingsAnalyseAndroid.API;

import org.json.JSONException;

public interface HttpResponseCallBack {
    void getResponse(String response) throws JSONException;
}

fragment

如需了解更多,请访问:https://www.emperinter.info/2022/02/14/how-to-get-data-from-okhttp-in-fragment/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值