我正在尝试从Web服务中获取一些数据到我的Android应用程序。我这样做是为了进行改装,但是我无法基于id获得特定的字符串(笑话)值。 我从以下URL获取数据:http://api.icndb.com/jokes/random/3。
当我在应用程序EditText中输入一个整数形式的ID时,我想使用该ID从上面提到的Web服务中检索一个具有特定ID的笑话。
例如,当我在我的应用程序EditText中输入179作为id并单击“开始改造”按钮时,我想从Web服务“ Chuck Norris?最喜欢的肉类是圆形烤架”中获取此String值。我想在我的应用程序中显示为敬酒消息的字符串值。
我已经准备了我的应用程序代码,但是我需要在接口@Query和@GET方法中进行一些更正,尤其是在回调接口成功方法中 我要在其中显示获取的数据。
我的应用程序具有下一个类:MainActivity.class,activity_main.xml,Joke.class(我使用POJO从JSON获取此类),JokeInterface。 我已经在build.gradle中的清单和“ implementation'com.squareup.retrofit:retrofit:1.9.0'”中输入了Internet许可作为依赖。
我在MainActivity.class和JokeInterface.class中添加了注释,我认为需要输入一些代码才能完成任务。
这是我的代码:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@android:color/holo_blue_bright"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text="Joke Id" />
<EditText
android:id="@+id/etEnterId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:gravity="center"
android:ems="10"
android:hint="Enter id"
android:inputType="number" />
<Button
android:id="@+id/btnStartRetrofit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="188dp"
android:text="Start Retrofit" />
</RelativeLayout>
MainActivity.class:
package com.example.dezox.restwebservis;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.dezox.restwebservis.model.Joke;
import java.util.List;
import retrofit.RestAdapter;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class MainActivity extends AppCompatActivity {
private EditText etEnterId;
private Button btnStartRetrofit;
private JokeInterface jokeInterface;
private Callback<Joke> call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidgets();
setupListener();
setupRestAdapter();
}
private void initWidgets() {
etEnterId = findViewById(R.id.etEnterId);
btnStartRetrofit = findViewById(R.id.btnStartRetrofit);
}
private void setupListener() {
btnStartRetrofit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (etEnterId.getText().length() > 0){
int id = Integer.parseInt(etEnterId.getText().toString());
getJokeMethod(id);
}
}
});
}
private void getJokeMethod(int id) {
jokeInterface.getJoke(id, call);
}
private void setupRestAdapter(){
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(JokeInterface.ENDPOINT_URL).build();
jokeInterface = restAdapter.create(JokeInterface.class);
call = new Callback<Joke>() {
@Override
public void success(Joke joke, Response response) {
StringBuilder builder = new StringBuilder();
builder.append(joke.getType()+"\n");
/*NOTE:
Here I need some code for a showing the string data in a toast message*/
Toast.makeText(getApplicationContext(), builder, Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
}
};
}
}
笑话课:(POJO)
package com.example.dezox.restwebservis.model;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Joke {
@SerializedName("type")
@Expose
private String type;
@SerializedName("value")
@Expose
private List<Value> value = null;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Value> getValue() {
return value;
}
public void setValue(List<Value> value) {
this.value = value;
}
}
class Value {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("joke")
@Expose
private String joke;
@SerializedName("categories")
@Expose
private List<String> categories = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getJoke() {
return joke;
}
public void setJoke(String joke) {
this.joke = joke;
}
public List<String> getCategories() {
return categories;
}
public void setCategories(List<String> categories) {
this.categories = categories;
}
}
JokeInterface.class:
package com.example.dezox.restwebservis;
import com.example.dezox.restwebservis.model.Joke;
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Query;
public interface JokeInterface {
public static final String ENDPOINT_URL = "http://api.icndb.com/";
@GET("/")
void getJoke(@Query("") int id, Callback<Joke> callback);
/*NOTE:
Here I need some code in GET and Query in order to fetch a string
joke with specific id.*/
}
答案 0 :(得分:1)
现在,我发现,您正在调用的url返回了笑话列表。 “ http://api.icndb.com/jokes/random/3”的意思是-您将获得3个随机笑话的对象。因此,如果您打出“ http://api.icndb.com/jokes/random/179之类的电话,那么您将获得179个随机笑话的列表。在这种情况下,3和179不是id,它们表示对象计数。
如果您一次只想讲一个具有特定ID的笑话,则可以像“ http://api.icndb.com/jokes/179”那样调用,这将为您提供--
{ "type": "success", "value": { "id": 179, "joke": "Chuck Norris? favourite cut of meat is the roundhouse.", "categories": [] } }
“笑话”(POJO类)中的“值”将不再列出,
@SerializedName("value")
@Expose
private Value value;
呼叫为
@GET("jokes/{id}")
void getJoke(@Path("id") int id, Callback<Joke> callback);
您可以显示返回的笑话值,
@Override
public void success(Joke joke, Response response) {
Toast.makeText(getApplicationContext(), joke.getValue().getJoke(), Toast.LENGTH_LONG).show();
}
希望得到帮助!