साधारण अनुरोध भेजें

अपने Android ऐप्लिकेशन में नेटवर्क से जुड़े काम करने के लिए, Cronet लाइब्रेरी इस्तेमाल करने का तरीका जानें. Cronet, Chromium नेटवर्क स्टैक है. इसे लाइब्रेरी के तौर पर उपलब्ध कराया गया है, ताकि आप इसे अपने ऐप्लिकेशन में इस्तेमाल कर सकें. लाइब्रेरी की सुविधाओं के बारे में ज़्यादा जानकारी के लिए, Cronet का इस्तेमाल करके नेटवर्क से जुड़े काम करना लेख पढ़ें.

अपने प्रोजेक्ट में लाइब्रेरी सेट अप करना

अपने प्रोजेक्ट में Cronet लाइब्रेरी में डिपेंडेंसी जोड़ने के लिए, यह तरीका अपनाएं:

  1. पुष्टि करें कि Android Studio ने आपके प्रोजेक्ट की settings.gradle फ़ाइल में, Google की Maven रिपॉज़िटरी का रेफ़रंस शामिल किया है. इसके लिए, यहां दिया गया उदाहरण देखें:

    Groovy

    dependencyResolutionManagement {
       ...
       repositories {
           ...
           google()
       }
    }

    Kotlin

    dependencyResolutionManagement {
       ...
       repositories {
           ...
           google()
       }
    }
  2. अपने ऐप्लिकेशन मॉड्यूल की build.gradle फ़ाइल के dependencies सेक्शन में, Cronet के लिए Google Play services की क्लाइंट लाइब्रेरी का रेफ़रंस शामिल करें. इसके लिए, यहां दिया गया उदाहरण देखें:

    Groovy

    dependencies {
       implementation 'com.google.android.gms:play-services-cronet:18.0.1'
    }

    Kotlin

    dependencies {
       implementation("com.google.android.gms:play-services-cronet:18.0.1")
    }

इस डिपेंडेंसी को जोड़ने के बाद बनाए गए CronetEngine ऑब्जेक्ट, Google Play services से लोड किए गए Cronet का इस्तेमाल करते हैं. CronetEngine बनाते समय, अचानक होने वाली गड़बड़ियों से बचने के लिए (जैसे, उन डिवाइसों पर जिन्हें Google Play services के अपडेट किए गए वर्शन की ज़रूरत होती है), CronetEngine ऑब्जेक्ट बनाने से पहले CronetProviderInstaller.installProvider(Context) को कॉल करें.

अगर Cronet को Google Play services से लोड नहीं किया जा सकता, तो Cronet के एपीआई को लागू करने का एक और तरीका है. हालांकि, यह तरीका कम असरदार है. इस फ़ॉलबैक इंप्लिमेंटेशन का इस्तेमाल करने के लिए, org.chromium.net:cronet-fallback पर निर्भर रहें और new JavaCronetProvider(context).createBuilder() को कॉल करें.

नेटवर्क का अनुरोध करना

इस सेक्शन में, Cronet लाइब्रेरी का इस्तेमाल करके नेटवर्क का अनुरोध बनाने और भेजने का तरीका बताया गया है. नेटवर्क का अनुरोध भेजने के बाद, आपके ऐप्लिकेशन को नेटवर्क के रिस्पॉन्स को प्रोसेस करना होगा.

CronetEngine का इंस्टेंस बनाना और उसे कॉन्फ़िगर करना

लाइब्रेरी, CronetEngine.Builder क्लास उपलब्ध कराती है. इसका इस्तेमाल करके, CronetEngine का इंस्टेंस बनाया जा सकता है. यहां दिए गए उदाहरण में, CronetEngine ऑब्जेक्ट बनाने का तरीका बताया गया है:

Kotlin

val myBuilder = CronetEngine.Builder(context)
val cronetEngine: CronetEngine = myBuilder.build()

Java

CronetEngine.Builder myBuilder = new CronetEngine.Builder(context);
CronetEngine cronetEngine = myBuilder.build();

CronetEngine ऑब्जेक्ट को कॉन्फ़िगर करने के लिए, Builder क्लास का इस्तेमाल किया जा सकता है. उदाहरण के लिए, कैश मेमोरी और डेटा कंप्रेशन जैसे विकल्प दिए जा सकते हैं. ज़्यादा जानकारी के लिए, CronetEngine.Builder देखें.

अनुरोध के कॉलबैक को लागू करने का तरीका बताएं

कॉलबैक को लागू करने के लिए, UrlRequest.Callback की सबक्लास बनाएं और ज़रूरी ऐब्स्ट्रैक्ट तरीके लागू करें. इसका उदाहरण यहां दिया गया है:

Kotlin

private const val TAG = "MyUrlRequestCallback"

class MyUrlRequestCallback : UrlRequest.Callback() {
    override fun onRedirectReceived(request: UrlRequest?, info: UrlResponseInfo?, newLocationUrl: String?) {
        Log.i(TAG, "onRedirectReceived method called.")
        // You should call the request.followRedirect() method to continue
        // processing the request.
        request?.followRedirect()
    }

    override fun onResponseStarted(request: UrlRequest?, info: UrlResponseInfo?) {
        Log.i(TAG, "onResponseStarted method called.")
        // You should call the request.read() method before the request can be
        // further processed. The following instruction provides a ByteBuffer object
        // with a capacity of 102400 bytes for the read() method. The same buffer
        // with data is passed to the onReadCompleted() method.
        request?.read(ByteBuffer.allocateDirect(102400))
    }

    override fun onReadCompleted(request: UrlRequest?, info: UrlResponseInfo?, byteBuffer: ByteBuffer?) {
        Log.i(TAG, "onReadCompleted method called.")
        // You should keep reading the request until there's no more data.
        byteBuffer?.clear()
        request?.read(byteBuffer)
    }

    override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
        Log.i(TAG, "onSucceeded method called.")
    }
}

Java

class MyUrlRequestCallback extends UrlRequest.Callback {
  private static final String TAG = "MyUrlRequestCallback";

  @Override
  public void onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl) {
    Log.i(TAG, "onRedirectReceived method called.");
    // You should call the request.followRedirect() method to continue
    // processing the request.
    request.followRedirect();
  }

  @Override
  public void onResponseStarted(UrlRequest request, UrlResponseInfo info) {
    Log.i(TAG, "onResponseStarted method called.");
    // You should call the request.read() method before the request can be
    // further processed. The following instruction provides a ByteBuffer object
    // with a capacity of 102400 bytes for the read() method. The same buffer
    // with data is passed to the onReadCompleted() method.
    request.read(ByteBuffer.allocateDirect(102400));
  }

  @Override
  public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
    Log.i(TAG, "onReadCompleted method called.");
    // You should keep reading the request until there's no more data.
    byteBuffer.clear();
    request.read(byteBuffer);
  }

  @Override
  public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
    Log.i(TAG, "onSucceeded method called.");
  }
}

नेटवर्क टास्क मैनेज करने के लिए, Executor ऑब्जेक्ट बनाएं

नेटवर्क से जुड़े टास्क चलाने के लिए, Executor क्लास का इस्तेमाल किया जा सकता है. Executor का इंस्टेंस पाने के लिए, Executors क्लास के किसी स्टैटिक तरीके का इस्तेमाल करें. इससे Executor ऑब्जेक्ट मिलता है. यहां दिए गए उदाहरण में, newSingleThreadExecutor() तरीके का इस्तेमाल करके, Executor ऑब्जेक्ट बनाने का तरीका बताया गया है:

Kotlin

val executor: Executor = Executors.newSingleThreadExecutor()

Java

Executor executor = Executors.newSingleThreadExecutor();

UrlRequest ऑब्जेक्ट बनाना और उसे कॉन्फ़िगर करना

नेटवर्क का अनुरोध करने के लिए, CronetEngine के newUrlRequestBuilder() तरीके को कॉल करें. इसके लिए, डेस्टिनेशन यूआरएल, कॉलबैक क्लास का इंस्टेंस, और एक्ज़ीक्यूटर ऑब्जेक्ट पास करें. newUrlRequestBuilder() मेथड, UrlRequest.Builder ऑब्जेक्ट दिखाता है. इसका इस्तेमाल करके, UrlRequest ऑब्जेक्ट बनाया जा सकता है, जैसा कि इस उदाहरण में दिखाया गया है:

Kotlin

val requestBuilder = cronetEngine.newUrlRequestBuilder(
        "https://www.example.com",
        MyUrlRequestCallback(),
        executor
)

val request: UrlRequest = requestBuilder.build()

Java

UrlRequest.Builder requestBuilder = cronetEngine.newUrlRequestBuilder(
        "https://www.example.com", new MyUrlRequestCallback(), executor);

UrlRequest request = requestBuilder.build();

UrlRequest के इंस्टेंस को कॉन्फ़िगर करने के लिए, Builder क्लास का इस्तेमाल किया जा सकता है. उदाहरण के लिए, प्राथमिकता या एचटीटीपी वर्ब तय किया जा सकता है. ज़्यादा जानकारी के लिए, UrlRequest.Builder देखें.

नेटवर्क टास्क शुरू करने के लिए, अनुरोध का start() तरीका कॉल करें:

Kotlin

request.start()

Java

request.start();

इस सेक्शन में दिए गए निर्देशों का पालन करके, Cronet का इस्तेमाल करके नेटवर्क का अनुरोध बनाया और भेजा जा सकता है. हालांकि, इसे आसान बनाने के लिए, UrlRequest.Callback को लागू करने का उदाहरण सिर्फ़ लॉग में एक मैसेज प्रिंट करता है. यहां दिए गए सेक्शन में, कॉलबैक लागू करने का तरीका बताया गया है. इससे ज़्यादा काम के मामलों में मदद मिलती है. जैसे, जवाब से डेटा निकालना और अनुरोध में गड़बड़ी का पता लगाना.

नेटवर्क से मिले जवाब को प्रोसेस करना

start() तरीके को कॉल करने के बाद, Cronet अनुरोध की लाइफ़साइकल शुरू करता है. लाइफ़साइकल के दौरान, कॉलबैक तय करके अनुरोध को मैनेज करें. लाइफ़साइकल के बारे में ज़्यादा जानकारी के लिए, Cronet अनुरोध का लाइफ़साइकल देखें. UrlRequest.Callback की सबक्लास बनाकर और इन तरीकों को लागू करके, कॉलबैक तय किया जा सकता है:

onRedirectReceived()

Cronet इस तरीके को तब लागू करता है, जब सर्वर मूल अनुरोध के जवाब में एचटीटीपी रीडायरेक्ट कोड जारी करता है. नए डेस्टिनेशन पर रीडायरेक्ट करने के लिए, followRedirect() तरीके का इस्तेमाल करें. इसके अलावा, cancel() तरीके का इस्तेमाल करें. यहां दिए गए उदाहरण में, इस तरीके को लागू करने का तरीका बताया गया है:

Kotlin

override fun onRedirectReceived(request: UrlRequest?, info: UrlResponseInfo?, newLocationUrl: String?) {
  // Determine whether you want to follow the redirect.
  ...

  if (shouldFollow) {
      request?.followRedirect()
  } else {
      request?.cancel()
  }
}

Java

@Override
public void onRedirectReceived(UrlRequest request, UrlResponseInfo info, String newLocationUrl) {
  // Determine whether you want to follow the redirect.
  ...

  if (shouldFollow) {
    request.followRedirect();
  } else {
    request.cancel();
  }
}
onResponseStarted()

हेडर का आखिरी सेट मिलने पर, Cronet इस तरीके को लागू करता है. सभी रीडायरेक्ट के बाद, Cronet सिर्फ़ onResponseStarted() को शुरू करता है. यहां दिए गए कोड में, इस तरीके को लागू करने का एक उदाहरण दिखाया गया है:

Kotlin

override fun onResponseStarted(request: UrlRequest?, info: UrlResponseInfo?) {
  val httpStatusCode = info?.httpStatusCode
  if (httpStatusCode == 200) {
    // The request was fulfilled. Start reading the response.
    request?.read(myBuffer)
  } else if (httpStatusCode == 503) {
    // The service is unavailable. You should still check if the request
    // contains some data.
    request?.read(myBuffer)
  }
  responseHeaders = info?.allHeaders
}

Java

@Override
public void onResponseStarted(UrlRequest request, UrlResponseInfo info) {
  int httpStatusCode = info.getHttpStatusCode();
  if (httpStatusCode == 200) {
    // The request was fulfilled. Start reading the response.
    request.read(myBuffer);
  } else if (httpStatusCode == 503) {
    // The service is unavailable. You should still check if the request
    // contains some data.
    request.read(myBuffer);
  }
  responseHeaders = info.getAllHeaders();
}
onReadCompleted()

Cronet इस तरीके को तब लागू करता है, जब रिस्पॉन्स बॉडी का कुछ हिस्सा पढ़ लिया जाता है. यहां दिए गए कोड के उदाहरण में, इस तरीके को लागू करने और रिस्पॉन्स बॉडी को निकालने का तरीका बताया गया है:

Kotlin

override fun onReadCompleted(request: UrlRequest?, info: UrlResponseInfo?, byteBuffer: ByteBuffer?) {
  // The response body is available, process byteBuffer.
  ...

  // Continue reading the response body by reusing the same buffer
  // until the response has been completed.
  byteBuffer?.clear()
  request?.read(myBuffer)
}

Java

@Override
public void onReadCompleted(UrlRequest request, UrlResponseInfo info, ByteBuffer byteBuffer) {
  // The response body is available, process byteBuffer.
  ...

  // Continue reading the response body by reusing the same buffer
  // until the response has been completed.
  byteBuffer.clear();
  request.read(myBuffer);
}
onSucceeded()

नेटवर्क का अनुरोध पूरा होने पर, Cronet इस तरीके को लागू करता है. यहां दिए गए उदाहरण में, इस तरीके को लागू करने का तरीका बताया गया है:

Kotlin

override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) {
    // The request has completed successfully.
}

Java

@Override
public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
  // The request has completed successfully.
}
onFailed()

अगर आपका ऐप्लिकेशन start() तरीके को कॉल करता है और इसके बाद किसी वजह से अनुरोध पूरा नहीं हो पाता है, तो Cronet इस तरीके को कॉल करता है. यहां दिए गए उदाहरण में, इस तरीके को लागू करने और गड़बड़ी के बारे में जानकारी पाने का तरीका बताया गया है:

Kotlin

override fun onFailed(request: UrlRequest?, info: UrlResponseInfo?, error: CronetException?) {
    // The request has failed. If possible, handle the error.
    Log.e(TAG, "The request failed.", error)
}

Java

@Override
public void onFailed(UrlRequest request, UrlResponseInfo info, CronetException error) {
  // The request has failed. If possible, handle the error.
  Log.e(TAG, "The request failed.", error);
}
onCanceled()

अगर cancel() तरीके का इस्तेमाल करके अनुरोध रद्द किया गया था, तो Cronet इस तरीके को लागू करता है. Cronet के onCanceled() को शुरू करने के बाद, यह UrlRequest.Callback क्लास के किसी अन्य तरीके को शुरू नहीं करता. इस तरीके का इस्तेमाल करके, किसी अनुरोध को प्रोसेस करने के लिए असाइन किए गए संसाधनों को खाली किया जा सकता है. यहां दिए गए उदाहरण में, इस तरीके को लागू करने का तरीका बताया गया है:

Kotlin

override fun onCanceled(request: UrlRequest?, info: UrlResponseInfo?) {
    // Free resources allocated to process this request.
    ...
}

Java

@Override
public void onCanceled(UrlRequest request, UrlResponseInfo info) {
  // Free resources allocated to process this request.
  ...
}