2017-04-14 39 views
0

我想從我的Android項目使用改進將數據發送到API。一切似乎沒有錯誤的工作,但沒有http請求離開應用程序。我可以用Wireshark篩選和API控制檯日誌來證實這一點。這裏是我的應用程序的這一部分的示例僞代碼:沒有HTTP請求是從改造發送/ Android/

// sample code 

findViewById(R.id.submit_btn).setOnClickListener(this); 

@Override 
public void onClick(View v) { 
    int i = v.getId(); 
    if (i == R.id.submit_btn){ 
     Intent intent = new Intent(CurrentActivity.this, HomeActivity.class); 

     // myObj is class storing several values and it is defined in separate class 
     MyObj obj = new MyObj(** some attributes here **); 

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("http://api.address") 
       .client(new OkHttpClient()) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     MyAPI api = retrofit.create(MyAPI.class); 

     Call<Void> upload = api.newObj(obj); 

     startActivity(intent); 
     finish(); 
    } 

不知道我錯過了什麼。有任何想法嗎?

P.S.這裏是應用程序使用該部分的依賴項:

compile 'com.squareup.retrofit2:retrofit:2.1.0' 
compile 'com.google.code.gson:gson:2.6.2' 
compile 'com.squareup.retrofit2:converter-gson:2.1.0' 
compile 'com.squareup.okhttp3:okhttp:3.0.1' 

回答

1

這隻準備請求,不發送它。

Call<Void> upload = api.newObj(obj); 

嘗試使upload.enqueue()

+0

謝謝,這解決了這個問題。改造官方的例子不夠詳細。當你提到排隊時,我找到了原始文檔。如果需要,以下是其他人的鏈接:https://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html#enqueue-retrofit2.Callback- – nikitz

+0

這是實際的文檔。 https://github.com/square/retrofit/wiki/Retrofit-Tutorials –

1

你永遠不排隊或執行調用,執行到服務器使用upload.enqueue(new CallBack here)異步調用執行同步立即使用upload.execute()