2017-09-14 44 views
1

有人可以幫助我理解這段代碼。在此先感謝。OKHttpClient的目的是什麼?這段代碼有沒有其他選擇?將PHP與Android Studio鏈接以獲取Firebase令牌

private void registerToken(String token) { 

    OkHttpClient client = new OkHttpClient(); 
    RequestBody body = new FormBody.Builder() 
      .add("Token",token) 
      .build(); 

    Request request = new Request.Builder() 
      .url("notify.php") 
      .post(body) 
      .build(); 

    try { 
     client.newCall(request).execute(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

回答

0

您可以使用loopj庫進行api調用。這是非常簡單的。按照Android的步驟。添加依賴於你的gradle這個

compile 'com.loopj.android:android-async-http:1.4.9' 

請求您的網絡API

String url="Your url here"; 
    RequestParams requestParams = new RequestParams(); 

     requestParams.put("Token", userToken); 
    new AsyncHttpClient().post(url,requestParams, new AsyncHttpResponseHandler() { 
     @Override 
     public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 
      String rs = new String(responseBody); 



     @Override 
     public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 

     } 
    });