开发者社区> 问答> 正文

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

我正在尝试获取电影剧情或通过omdb webservice发布年份,当我键入电影名称时,我会收到此错误消息(URL查询字符串不能包含replace块)。

这是我的代码。

主要活动


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>

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

展开
收起
Puppet 2020-01-18 10:14:27 773 0
1 条回答
写回答
取消 提交回答
  • 您应该使用@Query("id")而不是@Path("id")

    
    public interface ApiInterface {
    
    @GET("?t={id}&apikey=apikey(that's private)")
        Call<Post> getPost(@Query("id") String name);
    }
    
    2020-01-18 10:14:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Web应用系统性能优化 立即下载
高性能Web架构之缓存体系 立即下载
PWA:移动Web的现在与未来 立即下载