试图通过改型从Omdb Web服务获取帖子,但错误提示URL查询字符串不能包含替换块

问题描述 投票:0回答:1

[我试图通过omdb网络服务获取电影剧情或发行年份,当我输入电影名称时,我会收到此错误消息(URL查询字符串不能包含replace块。

这是我的代码。

MainActivity

public class MainActivity extends AppCompatActivity {

TextView tvText;

Retrofit retrofit;
ApiInterface apiInterface;
Call<Post> call;

EditText editText;
Button getButton;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvText=findViewById(R.id.tvText);
    editText=findViewById(R.id.editText);
    getButton=findViewById(R.id.getButton);


}

public void get(View view) {

    retrofit = new Retrofit.Builder()
            .baseUrl("http://www.omdbapi.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    apiInterface = retrofit.create(ApiInterface.class);

    call = apiInterface.getPost(editText.getText().toString());

    call.enqueue(new Callback<Post>() {
        @Override
        public void onResponse(Call<Post> call, Response<Post> response) {
            tvText.setText(response.body().getPlot());

        }

        @Override
        public void onFailure(Call<Post> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }

    });
}
}

接口

public interface ApiInterface {

@GET("?t={id}&apikey=apikey(that's private)")
Call<Post> getPost(@Path("id") String name);
}

发布json键的类

public class Post {

private int Year;
private String Plot;
private String Title;


public int getYear() {
    return Year;
}

public String getPlot() {
    return Plot;
}

public String getTitle() {
    return Title;
}
}

xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res    /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tvText"
    android:layout_width="324dp"
    android:layout_height="92dp"
    android:layout_marginTop="72dp"
    android:layout_marginRight="4dp"
    app:layout_constraintHorizontal_bias="0.535"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="88dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:ems="10"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/getButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="1dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="7dp"
    android:clickable="true"
    android:onClick="get"
    android:text="get"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvText" />

</android.support.constraint.ConstraintLayout>

...................................................... ........................................................ ...

android retrofit retrofit2
1个回答
0
投票

您应该使用@Query("id")而不是@Path("id")

public interface ApiInterface {

@GET("?t={id}&apikey=apikey(that's private)")
    Call<Post> getPost(@Query("id") String name);
}
© www.soinside.com 2019 - 2024. All rights reserved.