gpt4 book ai didi

java - 任何人都可以帮助我通过改造从网络服务中获取数据吗?

转载 作者:行者123 更新时间:2023-12-02 10:54:42 25 4
gpt4 key购买 nike

我正在尝试从网络服务获取一些数据到我的 Android 应用程序。我正在通过改造来做到这一点,但我无法根据 id 获取特定的字符串(笑话)值。我从这个 URL 获取数据:http://api.icndb.com/jokes/random/3

当我在应用程序 EditText 中输入一个整数 ID 时,我希望使用该 ID 从我上面提到的网络服务中检索具有特定 ID 的笑话。

例如,当我在应用程序 EditText 中输入 179 作为 id 并单击“开始改造”按钮时,我想从 Web 服务“Chuck Norris?最喜欢的肉 block 是圆形屋”获取此字符串值。我想在我的应用程序中将此字符串值显示为 Toast 消息。

我已经准备好了我的应用程序代码,但我需要在接口(interface) @Query 和 @GET 方法中进行一些更正,特别是在回调接口(interface)成功方法中我想在其中显示获取的数据。

我的应用程序有以下类:MainActivity.class、activity_main.xml、Joke.class(我使用 POJO 从 JSON 获取此类)、JokeInterface。我已经在 list 中输入了互联网权限,并在我的 build.gradle 中输入了“implementation 'com.squareup.retrofit:retrofit:1.9.0'”作为依赖项。

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

这是我的代码:

Activity 主线:

<?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) {

}
};

}


}

笑话.class:(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.*/

}

最佳答案

现在我发现,您调用的网址返回了笑话列表。“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": [] } }

并且“Joke”(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();
}

希望这有帮助!

关于java - 任何人都可以帮助我通过改造从网络服务中获取数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51862666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com