如何将数据从 activity 发送到接口

how to send data from an activity to interface

我有一个 activity。这让我得到了一个 user id 。我想把它放到一个界面中,(对我来说也是一个愚蠢的问题)

例如。 代码片段。 (展示我想要完成的事情)

ACTIVITY

class MainActivity extends Activity{

         public static string user;
         public void onCreate(Bundle savedInstanceState)
         {
                super.onCreate(savedInstanceState);
                user="1";  //I WANT TO SEND THIS VALUE TO MY INTERFACE SHOWN BELOW
         }
}     

接口

package random.test.done;

import retrofit2.Call;
import retrofit2.http.GET;

public interface RequestExample{

    @GET("claim.php?id=1" + MainActivity.user); /// how to get that user data here ? 
    Call<JSONResponseClaim> getJSONClaim();
}

@GET("claim.php?id=1" + MainActivity.user); // 当悬停在 MainActivity.user 上时,界面中的这一行会出错(属性值必须是常量)

问题在上面代码段的注释里

首先你应该修正你的RequestExample方法

@GET("/claim.php")
Call<JSONResponseClaim> getJSONClaim(
    @Query("id") Integer id,
    @Query("user") String user
);

然后像这样在你的 activity 上使用它

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://myendpoint.com")
    .build();

RequestExample service = retrofit.create(RequestExample.class);

Call<JSONResponseClaim> call = service.getJSONClaim(1, user);

希望对您有所帮助。

此外,如果您想检查一些样本:

https://github.com/square/retrofit/tree/master/samples/src/main/java/com/example/retrofit