- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是跟着例子: https://www.learn2crack.com/2016/02/recyclerview-json-parsing.html它工作得很好..我想解析
[
{
"song_name": "Hero",
"song_id": "1990",
"artist_name": "Enrique"
},{
"song_name": "African Queen",
"song_id": "2004",
"artist_name": "Tuface"
}, {
"song_name": "Ifunanyi",
"song_id": "2012",
"artist_name": "PSquare"
}
]
这是我的代码变得空白..我是 android 的新手给我一些我想要的其他想法..任何人都请指导我
公共(public)类 MainActivity 扩展 AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<ItemObject> data;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
loadJSON();
}
private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://toscanyacademy.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
adapter = new MyAdapter(data);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
我的请求接口(interface):
public interface RequestInterface {
@GET("blog/mp.php")
Call<JSONResponse> getJSON();
}
JSON 响应:
public class JSONResponse {
private ItemObject[] obj;
public ItemObject[] getAndroid() {
return obj;
}
}
public class ItemObject {
public String getSong_name() {
return song_name;
}
public String getSong_id() {
return song_id;
}
public String getArtist_name() {
return artist_name;
}
private String song_name,song_id,artist_name;
}
这是我的适配器:
公共(public)类 MyAdapter 扩展 RecyclerView.Adapter {
private ArrayList<ItemObject> users;
public MyAdapter(ArrayList<ItemObject> first) {
this.users = first;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
viewHolder.song_title.setText(users.get(i).getsong_name());
viewHolder.song_id.setText(users.get(i).getsong_id());
viewHolder.song_author.setText(users.get(i).getartist_name());
}
@Override
public int getItemCount() {
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView song_title, song_id, song_author;
public ViewHolder(View view) {
super(view);
song_title = (TextView) view.findViewById(R.id.song_name);
song_id = (TextView) view.findViewById(R.id.song_id);
song_author=(TextView) view.findViewById(R.id.artist_name);
}
}
行布局:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/song_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000"
/>
<TextView
android:id="@+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#000" />
最佳答案
创建您的 POJO 类:例子.java
@Generated("org.jsonschema2pojo")
public class Example {
@SerializedName("song_name")
@Expose
private String songName;
@SerializedName("song_id")
@Expose
private String songId;
@SerializedName("artist_name")
@Expose
private String artistName;
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
this.songName = songName;
}
public String getSongId() {
return songId;
}
public void setSongId(String songId) {
this.songId = songId;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
}
在您的界面中:
public interface RequestInterface {
@GET("blog/mp.php")
Call<List<Example>> getJSON();
}
在你的 Activity 中:
ArrayList<Example> arrayList = new ArrayList<>();
//Inside loadJSON()
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://toscanyacademy.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface request = retrofit.create(RequestInterface.class);
Call<List<Example>> listCall = request.getJSON();
listCall.enqueue(new Callback<List<Example>>() {
@Override
public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
for (int i = 0; i < response.body().size(); i++) {
Example example = new Example();
example.setArtistName(response.body().get(i).getArtistName());
example.setSongId(response.body().get(i).getSongId());
example.setSongName(response.body().get(i).getSongName());
arrayList.add(example);
}
adapter = new MyAdapter(arrayList);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<Example>> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
关于Android Retrofit解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38396630/
我正在尝试迁移我的应用程序以使用 RxJava。我已经在使用 Retrofit,因此我正在尝试使用方法返回 Observables 的 Retrofit 接口(interface)。但是我现在在针对它
我想 post 数据如下: { "user_id":"14545646", "list":["4545645","4545645","4545645","4545645"]
我是改造新手。我向网站发出 POST 请求。网站以 HTML 形式返回响应。所以我会解析它。但是 Retrofit 尝试将其解析为 JSON。怎么办? @FormUrlEncoded @POST("/
我想异步执行 2 个网络调用 - 我正在使用 Retrofit+RxJava 来完成这个。这个逻辑来自一个简单的 Runner 类来测试解决方案。注意:这主要涉及服务器端的 RxJava。 我的代码如
我最近在改造的存储库中使用 Flow。 Result 的密封类 enum class ApiStatus{ SUCCESS, ERROR, LOADING } sealed c
我目前正在开发 Retrofit2 客户端的错误处理部分(使用 Retrofit 的 Rx 实现)。 找了一段时间,没有找到简单的方法将Retrofit返回的ResponseBody反序列化到我报错的
我从 retrofit 更新到 retrofit2 后出现此错误。 FATAL EXCEPTION: OkHttp Dispatcher Process: nz.co.datacom.mars.jun
在使用 RxJava 和 Retrofit 2 时,我正在尝试创建单元测试来覆盖我的应用何时收到特定响应。 我遇到的问题是,在 Retrofit 2 中,我看不到在不使用反射的情况下创建 retrof
在 Retrofit 1.9.x 中有一个 RetrofitError.Kind.NETWORK这让您可以轻松确定故障是否由网络错误引起。在新的 2.0 API 中,我们不再有 RetrofitErr
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
在项目上安装这个库之后: compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0
在 Retrofit 1.x 中,我使用以下模式创建 API 服务类,该类模拟某些构建变体的不良网络连接。 // Retrofit 1 private T create(Class apiServi
Retrofit请求API格式(Android): @POST("getOrderStatus") @Headers("Content-Type:application/json") Obser
当我实例化 RestAdapter 时,我的应用程序总是崩溃 private void submitForm(SignupForm form){ RestAdapter adapter = n
我正要从 retrofit 1.9 迁移到最新版本并遇到问题... 我的设置: 三星 Galaxy S7 Edge(Android 7) 改造 2.3 OkHttp 3.8 迁移后突然遇到这个问题:
我正在尝试在 2.5.1-SNAPSHOT 中使用 retrofit 的协程支持,但我不断收到一个奇怪的异常。 我的改造服务类有: @GET("weather") suspend fun getFor
我正在尝试 Ktor通过转换一些当前正在使用的现有项目 Retrofit . 虽然我可以很容易地将请求转换为: client.get { url("$BASE_URL/something/so
使用改造 2,如何为上传的文件设置动态名称? 目前是这样的: @Part("avatar\"; filename=\"image\" ") RequestBody image, 但是,上传的文件名将是
我是一名优秀的程序员,十分优秀!