2017-09-05 21 views
0

我很困惑,請你能幫助我嗎?我讀過類似的問題,但對我來說並不清楚,因爲要提前感謝您的耐心和關注。如何從改造我的方法返回數據我很困惑?

我想返回到使用Retrofit從API調用中檢索到的onCreate數據。這裏是我調用Retrofit的功能。

私人無效loadTimeZoneAPI(雙緯度,經度兩倍,長時間戳,字符串apiKeyTz){

String lat = Double.toString(latitude); 
    String lon = Double.toString(longitude); 
    String time = Long.toString(timestamp); 

    serviceTZ.getDataTZ(lat+","+lon, time, apiKeyTz).enqueue(new Callback<TimeZoneGoogle>() { 
     @Override 
     public void onResponse(Call<TimeZoneGoogle> call, Response<TimeZoneGoogle> response) { 
      TimeZoneGoogle result = response.body(); 
      timeZone = result.getTimeZoneId(); 

     } 

     @Override 
     public void onFailure(Call<TimeZoneGoogle> call, Throwable t) { 

     } 
    }); 
} 

如何,我將返回區值,以在OnCreate在那裏我將用於計算。

+0

分享您的課堂,你想返回你有數據從服務器的數據和類 –

+0

@vikaskumar這是我的班級我的班級https://gist.github.com/ArnalShoorukov/8776041 dc998bafc1d9c8370f6a3760c –

回答

0

您需要一個回調監聽器來處理MainActivity中的數據或您用於查看的任何活動。 例如:使接口這樣

interface RetrofitListener{ 
     onDataLoad(TimeZoneGoogle timeZoneGoogle); 
    } 

然後給在活動的引用您從中調用改造類

public class ActivityTest extends AppCompatActivity implements ActivityTest.RetrofitListener { 

    RetrofitCall retrofitListener; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 
     retrofitListener = new RetrofitCall(this); 
    } 
} 

,並在這裏RetrofitCall類

private class RetrofitCall { 
     private RetrofitListener retrofitListener; 

     public RetrofitCall(RetrofitListener retrofitListener) { 
      this.retrofitListener = retrofitListener; 
     } 
void getData(){ 
    getDataTZ(lat+","+lon, time, apiKeyTz).enqueue(new Callback<TimeZoneGoogle>() { 
      @Override 
      public void onResponse(Call<TimeZoneGoogle> call, Response<TimeZoneGoogle> response) { 
       TimeZoneGoogle result = response.body(); 
       timeZone = result.getTimeZoneId(); 

      } 

      @Override 
      public void onFailure(Call<TimeZoneGoogle> call, Throwable t) { 

      } 
     }); 
} 
}