• Android使用jsonstring请求retrofit


    参考链接:https://www.jianshu.com/p/1e5200898c7b

    public void request(String jsonStr, ApiSucess sucess, ApiErr err) {
            try {
                JSONObject obj = new JSONObject(jsonStr);
                String url = obj.getString("url"); //   http://www.shabi.com/getinfo
                String method = obj.getString("method").toLowerCase();
                JSONObject data = obj.getJSONObject("data");
    
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
                String token = sp.getString("Authorization", "12345678");
    
                OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .addInterceptor(new Interceptor() {
                            @Override
                            public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                                Request original = chain.request();
                                Request.Builder requestBuilder = original.newBuilder()
                                        .header("Authorization", token)
                                        .method(original.method(), original.body());
                                Request request = requestBuilder.build();
                                return chain.proceed(request);
                            }
                        })
                        .connectTimeout(5, TimeUnit.SECONDS) // 超时 10s
                        .build();
    
                Retrofit retrofit = new Retrofit.Builder()
    //                    .baseUrl(baseUrl)
                        .baseUrl(getBaseUrl(url))
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                Server server = retrofit.create(Server.class);
                Call<ResponseBody> call;
                if (method.contains("get")) {
                    Map<String, String> queryMap = new HashMap<>();
                    Iterator<String> keys = data.keys();
                    while (keys.hasNext()) {
                        String key = keys.next();
                        queryMap.put(key, data.get(key).toString());
                    }
                    call = server.reqGet(getApiUri(url), queryMap);
                    enqueueMethod(call, sucess, err);
                } else if (method.contains("post")) {
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data.toString());
                    call = server.reqPost(getApiUri(url), body);
                    enqueueMethod(call, sucess, err);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private String getBaseUrl(String url) {
            String baseUrl;
            if (url.startsWith("http")) {
                int f = url.indexOf("//") + 2;
                String noHttp = url.substring(f);
                int i = noHttp.indexOf("/");
                int subIndex = f + i;
                baseUrl = url.substring(0, subIndex);
                String afterUri = url.substring(subIndex);
            } else {
                //  api.freego.haitiand.cn/travelcard/bindCard
                int i = url.indexOf("/");
                baseUrl = url.substring(0, i);
            }
            if(!baseUrl.contains("http")){
                baseUrl = "http://"+baseUrl ;
            }
            Log.i(TAG, "原生 baseUrl = " + baseUrl);
            return baseUrl;
        }
    
        private String getApiUri(String url) {
            String afterUri;
            if (url.startsWith("http")) {
                int f = url.indexOf("//") + 2;
                String noHttp = url.substring(f);
                int i = noHttp.indexOf("/");
                int subIndex = f + i;
                String baseUrl = url.substring(0, subIndex);
                afterUri = url.substring(subIndex);
            } else {
                //  api.freego.haitiand.cn/travelcard/bindCard
                int i = url.indexOf("/");
                afterUri = url.substring(i);
            }
            Log.i(TAG, "原生 afterUri = " + afterUri);
            return afterUri;
        }
    public interface Server {/*
        *   下边两个函数是对于Web调用的
        */
        @POST
        Call<ResponseBody> reqPost(@Url String url, @Body RequestBody requestBody);
    
        @GET
        Call<ResponseBody> reqGet(@Url String url, @QueryMap Map<String, String> queryMap);
    
    }
  • 相关阅读:
    Hbase集群部署及shell操作
    sqoop部署与使用
    azkaban部署
    Hive入门操作
    Hive部署
    MapReduce过程详解(基于hadoop2.x架构)
    MapReduce编程中常用的字符操作
    【图文详解】深入HDFS原理
    vue项目跨域问题的解决:vue-cli 开启代理
    beego框架入门(2)
  • 原文地址:https://www.cnblogs.com/Oldz/p/13438415.html
Copyright © 2020-2023  润新知