square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.09k stars 7.3k forks source link

please help me #1992

Closed Guolei1130 closed 8 years ago

Guolei1130 commented 8 years ago

this is error message.

application interceptor com.blueware.agent.android.instrumentation.okhttp3.d@41c1cec0 returned null

this is code

Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl("http://api.xxxx:8080")
                .addConverterFactory(SecurityGsonConverterFactory.create());
        OkHttpClient client = new OkHttpClient().newBuilder()
                .addInterceptor(new SecurityInterceptor())
                .build();
        Retrofit retrofit = builder.client(client).build();
        ;
        try {
            retrofit.create(UserApi.class).sign(
                    URLEncoder.encode(userName, "UTF-8").replace("*", "%2A").replace("+", "%20").replace("%7E", "~"),
                    URLEncoder.encode(pwd, "UTF-8").replace("*", "%2A").replace("+", "%20").replace("%7E", "~")
            ).enqueue(new retrofit2.Callback<USER>() {
                @Override
                public void onResponse(retrofit2.Call<USER> call, retrofit2.Response<USER> response) {

                }

                @Override
                public void onFailure(retrofit2.Call<USER> call, Throwable t) {

                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

and

@Override
    public Response intercept(Chain chain) throws IOException {

        String model = android.os.Build.MODEL;
        model = model.replaceAll("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……& amp;*()——+|{}【】‘;:”“’。,、?|-]", "");

        Request request = chain.request();
        RequestBody oldBody = request.body();
        Buffer buffer = new Buffer();
        oldBody.writeTo(buffer);
        StringBuffer stringBuffer = new StringBuffer();
        String s ;
        while((s = buffer.readUtf8Line()) != null){
            stringBuffer.append(s);
        }
        MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
        StringBuilder resultUrl = encrypt(stringBuffer.toString().trim());

        String newString = null;
        String signString = null;
        if (resultUrl.length()>0){
            int last = resultUrl.lastIndexOf("&");
            String str = resultUrl.substring(0, last);
            String timestamp = System.currentTimeMillis() / 1000 + "";
            String sign = HmacSHA256.getHmacSHA256(timestamp + str, UserAppConst.SIGN_KEY);
            signString = sign + "," + timestamp;
            newString = "x="+ XXTEA.encryptToBase64String(str, AppDataCenter.AppDataKey);
        }

        RequestBody body = RequestBody.create(mediaType, newString);
        request = request.newBuilder().
                header("Content-Type", body.contentType().toString()).
                header("Content-Length", String.valueOf(body.contentLength()))
                // TODO: 2016/8/23 添加其他header
                .header("X-ECAPI-Authorization", SESSION.getInstance().getToken())
                .header("X-ECAPI-UserAgent", "Platform/Android, Device/"+model+", Lang/"+ UserAgent.getInstance().lang+", ScreenWidth/"+UserAgent.getInstance().width+", ScreenHeight/"+UserAgent.getInstance().height)
                .header("X-ECAPI-UDID", UserAgent.getInstance().UDID)
                .header("X-ECAPI-Ver", UserAgent.getInstance().ver)
                .header("X-ECAPI-Sign",signString)
                .method(request.method(), body)
                .build();
        Log.e("test", "intercept: " + newString);
        Response response = chain.proceed(request);
        if (response == null){
            Log.e("test", "intercept: " + "null");
        }
        return response;
    }
JakeWharton commented 8 years ago

Your interceptor is returning null. This is not allowed by OkHttp.

On Wed, Aug 24, 2016, 10:54 PM guolei notifications@github.com wrote:

this is error message.

application interceptor com.blueware.agent.android.instrumentation.okhttp3.d@41c1cec0 returned null

this is code

Retrofit.Builder builder = new Retrofit.Builder() .baseUrl("http://api.xxxx:8080") .addConverterFactory(SecurityGsonConverterFactory.create()); OkHttpClient client = new OkHttpClient().newBuilder() .addInterceptor(new SecurityInterceptor()) .build(); Retrofit retrofit = builder.client(client).build(); ; try { retrofit.create(UserApi.class).sign( URLEncoder.encode(userName, "UTF-8").replace("", "%2A").replace("+", "%20").replace("%7E", "~"), URLEncoder.encode(pwd, "UTF-8").replace("", "%2A").replace("+", "%20").replace("%7E", "~") ).enqueue(new retrofit2.Callback() { @Override public void onResponse(retrofit2.Call call, retrofit2.Response response) {

            }

            @Override
            public void onFailure(retrofit2.Call<USER> call, Throwable t) {

            }
        });
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

and

@Override public Response intercept(Chain chain) throws IOException {

    String model = android.os.Build.MODEL;
    model = model.replaceAll("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……& amp;*()——+|{}【】‘;:”“’。,、?|-]", "");

    Request request = chain.request();
    RequestBody oldBody = request.body();
    Buffer buffer = new Buffer();
    oldBody.writeTo(buffer);
    StringBuffer stringBuffer = new StringBuffer();
    String s ;
    while((s = buffer.readUtf8Line()) != null){
        stringBuffer.append(s);
    }
    MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
    StringBuilder resultUrl = encrypt(stringBuffer.toString().trim());

    String newString = null;
    String signString = null;
    if (resultUrl.length()>0){
        int last = resultUrl.lastIndexOf("&");
        String str = resultUrl.substring(0, last);
        String timestamp = System.currentTimeMillis() / 1000 + "";
        String sign = HmacSHA256.getHmacSHA256(timestamp + str, UserAppConst.SIGN_KEY);
        signString = sign + "," + timestamp;
        newString = "x="+ XXTEA.encryptToBase64String(str, AppDataCenter.AppDataKey);
    }

    RequestBody body = RequestBody.create(mediaType, newString);
    request = request.newBuilder().
            header("Content-Type", body.contentType().toString()).
            header("Content-Length", String.valueOf(body.contentLength()))
            // TODO: 2016/8/23 添加其他header
            .header("X-ECAPI-Authorization", SESSION.getInstance().getToken())
            .header("X-ECAPI-UserAgent", "Platform/Android, Device/"+model+", Lang/"+ UserAgent.getInstance().lang+", ScreenWidth/"+UserAgent.getInstance().width+", ScreenHeight/"+UserAgent.getInstance().height)
            .header("X-ECAPI-UDID", UserAgent.getInstance().UDID)
            .header("X-ECAPI-Ver", UserAgent.getInstance().ver)
            .header("X-ECAPI-Sign",signString)
            .method(request.method(), body)
            .build();
    Log.e("test", "intercept: " + newString);
    Response response = chain.proceed(request);
    if (response == null){
        Log.e("test", "intercept: " + "null");
    }
    return response;
}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/retrofit/issues/1992, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEeBZfegZ0Z30xpwbVzfaQzwHh6wmks5qjQPggaJpZM4JspY_ .

JakeWharton commented 8 years ago

This isn't a Retrofit issue. That messages comes from OkHttp. Ensure your interceptors cannot return null which it doesn't allow.