Java 有人能帮我从改造后的web服务中获取数据吗?

Java 有人能帮我从改造后的web服务中获取数据吗?,java,android,rest,url,retrofit,Java,Android,Rest,Url,Retrofit,我正试图从网络服务中获取一些数据到我的android应用程序中。我是通过改装来实现这一点的,但我无法根据id获得具体的stringjoke值。 我从以下URL获取数据: 当我在我的应用程序EditText中输入一个id作为整数时,我想用这个id从我上面提到的web服务中检索一个带有特定id的笑话 例如,当我在我的应用程序EditText中输入179作为id并单击“开始改装”按钮时,我想从web服务Chuck Norris获得这个字符串值?最受欢迎的肉块是圆房子。我想在我的应用程序中将此字符串值显

我正试图从网络服务中获取一些数据到我的android应用程序中。我是通过改装来实现这一点的,但我无法根据id获得具体的stringjoke值。 我从以下URL获取数据:

当我在我的应用程序EditText中输入一个id作为整数时,我想用这个id从我上面提到的web服务中检索一个带有特定id的笑话

例如,当我在我的应用程序EditText中输入179作为id并单击“开始改装”按钮时,我想从web服务Chuck Norris获得这个字符串值?最受欢迎的肉块是圆房子。我想在我的应用程序中将此字符串值显示为toast消息

我已经准备好了我的应用程序代码,但我需要在接口@Query和@GET方法中进行一些更正,尤其是在回调接口success方法中 在这里,我要显示获取的数据

我的应用程序还有下一个类:MainActivity.class、activity_main.xml、Joke.classI使用POJO从JSON、JokeInterface获取这个类。 我已经在清单和实现'com.squareup.reformation:reformation:1.9.0'中作为依赖项输入了internet权限

我在MainActivity.class和JokeInterface.class中添加了一条注释,我认为需要在其中输入一些代码来完成任务

这是我的密码:

主要活动:

<?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.*/

        }

现在我发现,你正在调用的url返回了笑话列表。 意思-你得到3个随机笑话的对象。所以如果你打电话给like,你会得到179个随机笑话的列表。在本例中,3和179不是ID,而是对象计数

如果您一次只想要一个带有特定id的笑话,您可以调用like,这将为您提供如下输出-

{ "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);

希望这有帮助

您无法修改终结点??我尝试了许多可能的解决方案,但没有成功获取具有特定id的笑话字符串。我成功地从builder.appendjoke.getType+\n;从builder.appendjoke.getValue+\n;我不知道如何从特定id获取字符串笑话。我认为您应该在response@Queryid当您将值附加到url(如…/crooks/random?id=3)时使用。另外,您只提供了RestAdapter的主URL,您需要在接口方法中定义特定的路径,如@getjones/random/{id}。首先,我解决了这个问题,似乎@GET/crooks/random/{id}在没有/的情况下无法工作。第二个问题是我无法访问joke.getJoke,因为getJoke位于joke.class.see joke类中的Value类中。您可以像joke.getValue.getJoke一样尝试。正如我提到的,如果您调用../crooks/random/{id},您将以列表的形式获取值,并且您无法获取id为的特定对象,它将只返回随机对象。如果您调用…/croples/{id},您将获得值作为对象,并且您可以使用id获得所需的特定对象。您的问题是从响应中的数组中获取id…而不是从您获得的端点…@DezoGlad。看看这个url——这里我们每次都需要用欲望id替换179。这意味着我们正在替换URL的路径值。在这种情况下,我们可以使用@Pathid。但在这里,我们需要传递name和address的值,这些都是查询后的参数吗?签名,所以我们需要与@Queryname、@Queryaddress一起使用。您可以在这里的官方页面中找到更多信息-:
 @Override
        public void success(Joke joke, Response response) { 
            Toast.makeText(getApplicationContext(), joke.getValue().getJoke(), Toast.LENGTH_LONG).show();
        }