Java 改造中如何将变量传递到GET参数
我是java和Android新手。 我正在尝试将一些变量传递到改装GET调用中。但是我什么都不行,所以请你看看我的代码 要发送变量,我必须更改什么:Java 改造中如何将变量传递到GET参数,java,android,retrofit,parameter-passing,retrofit2,Java,Android,Retrofit,Parameter Passing,Retrofit2,我是java和Android新手。 我正在尝试将一些变量传递到改装GET调用中。但是我什么都不行,所以请你看看我的代码 要发送变量,我必须更改什么: myActuelFullName myOtherInfo 到服务器 我的java文件: public class Orte extends AppCompatActivity { ... @Override protected void onCreate(Bundle savedInstanceState) {
- myActuelFullName
- myOtherInfo
public class Orte extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orte);
Intent myIntent = getIntent(); // gets the previously created intent
myActuelFullName = myIntent.getStringExtra("paramFullName"); // will return "paramFullName"
getEntries(myActuelFullName);
}
private void getEntries(String myActuelFullName) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myDomain.de/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
String myOtherInfo = "testing123";
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries();
Log.d("DEBUG", myActuelFullName ); // at this point the Variable is known!
call.enqueue(new Callback<List<Entries>>() {
@Override
public void onResponse(Call<List<Entries>> call, Response<List<Entries>> response) {
if (!response.isSuccessful()) {
//textViewResult.setText("Code: " + response.code());
return;
}
//
@Override
public void onFailure(Call<List<Entries>> call, Throwable t) {
// textViewResult.setText(t.getMessage());
}
});
公共类Orte扩展了AppCompative活动{
...
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orte);
Intent myIntent=getIntent();//获取先前创建的意图
myActuelFullName=myIntent.getStringExtra(“paramFullName”);//将返回“paramFullName”
getEntries(myActuelFullName);
}
私有void getEntries(字符串myActuelFullName){
改装改装=新改装.Builder()
.baseUrl(“http://myDomain.de/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi JsonPlaceHolderApi=reformation.create(JsonPlaceHolderApi.class);
字符串myOtherInfo=“testing123”;
Call Call=jsonPlaceHolderApi.getEntries();
Log.d(“DEBUG”,myActuelFullName);//此时变量是已知的!
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
如果(!response.issusccessful()){
//textViewResult.setText(“代码:+response.Code());
返回;
}
//
@凌驾
失败时公共无效(调用调用,可丢弃的t){
//textViewResult.setText(t.getMessage());
}
});
和我的占位符Api:
public interface JsonPlaceHolderApi {
@GET("get_entries.php")
Call<List<Entries>> getEntries();
@POST("http://myDomain.de/api/mypost.php/")
Call<Post> createPost(@Body Post post);
}
公共接口JSONPlaceholder API{
@GET(“GET_entries.php”)
调用getEntries();
@职位(”http://myDomain.de/api/mypost.php/")
调用createPost(@Body Post);
}
您必须首先在API接口类中添加查询参数。添加两个查询参数后,您的类应该如下所示:
public interface JsonPlaceHolderApi {
@GET("get_entries.php")
Call<List<Entries>> getEntries(@Query("myActuelFullName") String myActuelFullName, @Query("myOtherInfo") String myOtherInfo);
@POST("http://myDomain.de/api/mypost.php/")
Call<Post> createPost(@Body Post post);
}
公共接口JSONPlaceholder API{
@GET(“GET_entries.php”)
调用getEntries(@Query(“myActuelFullName”)字符串myActuelFullName,@Query(“myOtherInfo”)字符串myOtherInfo);
@职位(”http://myDomain.de/api/mypost.php/")
调用createPost(@Body Post);
}
在活动中,函数调用应该如下所示:
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries(myActuelFullName,myOtherInfo);
Call Call=jsonPlaceHolderApi.getEntries(myActuelFullName,myOtherInfo);
试试看那里-