2016-01-14 17 views
15

में अनुरोध निकाय को संशोधित करना मैं एंड्रॉइड एप्लिकेशन में ओकेएचटीपी क्लाइंट के साथ रेट्रोफिट 2 (2.0.0-बीटा 3) का उपयोग कर रहा हूं और अब तक सब कुछ बढ़ रहा है। लेकिन वर्तमान में मुझे ओकेएचटीपी इंटरसेप्टर के साथ समस्या का सामना करना पड़ रहा है। जिस सर्वर से मैं संचार कर रहा हूं वह अनुरोध के निकाय में पहुंच टोकन ले रहा है, इसलिए जब मैं ऑथ टोकन जोड़ने या प्रमाणीकरणकर्ता विधि को प्रमाणित करने के अनुरोध को रोकता हूं, तो मुझे अद्यतन ऑथ टोकन जोड़ने की आवश्यकता होती है, तो मुझे इस उद्देश्य के लिए अनुरोध के शरीर को संशोधित करने की आवश्यकता है । लेकिन ऐसा लगता है कि मैं केवल हेडर में डेटा जोड़ सकता हूं लेकिन चालू अनुरोध के शरीर में नहीं।रेट्रोफिट 2: ओकेएचटीपी इंटरसेप्टर

client.interceptors().add(new Interceptor() { 
      @Override 
      public Response intercept(Chain chain) throws IOException { 
       Request request = chain.request(); 
       if (UserPreferences.ACCESS_TOKEN != null) { 
        // need to add this access token in request body as encoded form field instead of header 
        request = request.newBuilder() 
          .header("access_token", UserPreferences.ACCESS_TOKEN)) 
          .method(request.method(), request.body()) 
          .build(); 
       } 
       Response response = chain.proceed(request); 
       return response; 
      } 
     }); 

किसी को कैसे मेरे पहुँच टोकन (पहली बार या टोकन ताज़ा के बाद अद्यतन) जोड़ने के लिए अनुरोध शरीर को संशोधित करने के रूप में सही दिशा करने के लिए मुझसे बात कर सकते हैं: कोड मैं अब तक लिखा है इस प्रकार है? सही दिशा में किसी भी सूचक की सराहना की जाएगी।

उत्तर

16

मैं मौजूदा लोगों को पोस्ट पैरामीटर जोड़ने के लिए इसका उपयोग कर रहा हूं।

OkHttpClient client = new OkHttpClient.Builder() 
        .protocols(protocols) 
        .addInterceptor(new Interceptor() { 
         @Override 
         public Response intercept(Chain chain) throws IOException { 
          Request request = chain.request(); 
          Request.Builder requestBuilder = request.newBuilder(); 
RequestBody formBody = new FormEncodingBuilder() 
      .add("email", "[email protected]") 
      .add("tel", "90301171XX") 
      .build(); 
          String postBodyString = Utils.bodyToString(request.body()); 
          postBodyString += ((postBodyString.length() > 0) ? "&" : "") + Utils.bodyToString(formBody); 
          request = requestBuilder 
            .post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString)) 
            .build(); 
          return chain.proceed(request); 
         } 
        }) 
        .build(); 

public static String bodyToString(final RequestBody request){ 
     try { 
      final RequestBody copy = request; 
      final Buffer buffer = new Buffer(); 
      if(copy != null) 
       copy.writeTo(buffer); 
      else 
       return ""; 
      return buffer.readUtf8(); 
     } 
     catch (final IOException e) { 
      return "did not work"; 
     } 
    } 

OkHttp3:

RequestBody formBody = new FormBody.Builder() 
       .add("email", "[email protected]") 
       .add("tel", "90301171XX") 
       .build(); 
+0

यह एक अच्छा विचार 'bodyToString में बफर बंद करने के लिए()' –

+0

@ 3k thats की जरूरत नहीं लौटने से पहले, बफर नहीं करता है कुछ भी है कि निर्माता के अंदर बंद किया जा सकता का आवंटन किया जाएगा। https://github.com/square/okio/blob/master/okio/src/main/java/okio/Buffer.java#L59 – Fabian

1

इस के बाद से @Fabian द्वारा पिछले जवाब की टिप्पणी में लिखा नहीं किया जा सकता है, मैं अलग जवाब के रूप में यह एक पोस्टिंग कर रहा हूँ। यह उत्तर "एप्लिकेशन/जेसन" के साथ-साथ फॉर्म डेटा दोनों से संबंधित है।

import android.content.Context; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 

import okhttp3.FormBody; 
import okhttp3.Interceptor; 
import okhttp3.MediaType; 
import okhttp3.Request; 
import okhttp3.RequestBody; 
import okhttp3.Response; 
import okio.Buffer; 

/** 
* Created by debanjan on 16/4/17. 
*/ 

public class TokenInterceptor implements Interceptor { 
    private Context context; //This is here because I needed it for some other cause 

    //private static final String TOKEN_IDENTIFIER = "token_id"; 
    public TokenInterceptor(Context context) { 
     this.context = context; 
    } 

    @Override 
    public Response intercept(Chain chain) throws IOException { 
     Request request = chain.request(); 
     RequestBody requestBody = request.body(); 
     String token = "toku";//whatever or however you get it. 
     String subtype = requestBody.contentType().subtype(); 
     if(subtype.contains("json")){ 
      requestBody = processApplicationJsonRequestBody(requestBody, token); 
     } 
     else if(subtype.contains("form")){ 
      requestBody = processFormDataRequestBody(requestBody, token); 
     } 
     if(requestBody != null) { 
      Request.Builder requestBuilder = request.newBuilder(); 
      request = requestBuilder 
        .post(requestBody) 
        .build(); 
     } 

     return chain.proceed(request); 
    } 
    private String bodyToString(final RequestBody request){ 
     try { 
      final RequestBody copy = request; 
      final Buffer buffer = new Buffer(); 
      if(copy != null) 
       copy.writeTo(buffer); 
      else 
       return ""; 
      return buffer.readUtf8(); 
     } 
     catch (final IOException e) { 
      return "did not work"; 
     } 
    } 
    private RequestBody processApplicationJsonRequestBody(RequestBody requestBody,String token){ 
     String customReq = bodyToString(requestBody); 
     try { 
      JSONObject obj = new JSONObject(customReq); 
      obj.put("token", token); 
      return RequestBody.create(requestBody.contentType(), obj.toString()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    private RequestBody processFormDataRequestBody(RequestBody requestBody, String token){ 
     RequestBody formBody = new FormBody.Builder() 
       .add("token", token) 
       .build(); 
     String postBodyString = bodyToString(requestBody); 
     postBodyString += ((postBodyString.length() > 0) ? "&" : "") + bodyToString(formBody); 
     return RequestBody.create(requestBody.contentType(), postBodyString); 
    } 

} 
संबंधित मुद्दे