在 Json 改装屏幕上显示 ListView 时出现问题
Problems Showing ListView on Json Retrofit Screen
我有以下Json
信息,想拿出来Listview
:
{"result":[[[12,"01",1,"Fallo de corriente",0,1],
[12,"01",2,"Nivel m\u00E1ximo (activaci\u00F3n)",0,0],
[12,"01",3,"Nivel m\u00E1ximo(desactivaci\u00F3n)",0,1]]]
}
事实是应用程序给我一个错误,我没有提示,我是这样安装的:
我有POST
class如下:
package com.example.lista.Interface;
import java.util.List;
import java.lang.Integer;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Post {
@SerializedName("result")
@Expose
private List<List<List<Integer>>> result = null;
public List<List<List<Integer>>> getResult() {
return result;
}
public void setResult(List<List<List<Integer>>> result) {
this.result = result;
}
}
PostService 接口:
package com.example.lista.Interface;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
public interface PostService {
String API_ROUTE = " obtenerListaAlarmasSMS";
@GET(API_ROUTE)
Call<Post> getPost(@Header("Authorization") String credencialesEnBase64);
}
我的主要内容如下
package com.example.lista.Interface;
import android.os.Bundle;
import android.util.Log;
import android.util.Base64;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.lista.R;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.lang.String;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import static retrofit2.Response.error;
public class MainActivity extends AppCompatActivity {
ListView list;
List<List<List<List<Integer>>>> titles = new
ArrayList<List<List<List<Integer>>>>();
ArrayAdapter arrayAdapter;
String usuario = new String("");
String clave = new String("");
String error = new String("Algo ha fallado");
String credenciales = usuario + ":" + clave ;;
String credencialesEnBase64 = "Basic " +
Base64.encodeToString(credenciales.getBytes(), Base64.NO_WRAP);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, titles);
list = findViewById(R.id.list);
list.setAdapter(arrayAdapter);
getPosts();
}
private void getPosts() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.249:9096/datasnap/rest/TMetodosREST/")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostService postService = retrofit.create(PostService.class);
Call<Post> call = postService.getPost(credencialesEnBase64);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
for (Post post : response.body()) {
titles.add(post.getResult());
}
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.d("MainActivity", t.getMessage());
}
});
}
}
问题出在它告诉我的 OnResponse 中:
error: for-each not applicable to expression type
required: array or java.lang.Iterable
found: Post
我不知道是因为创建我的POJO的格式还是它发生了什么。
我想要的是获取ListView
里面的信息
As per your output
[12,"01",1,"Fallo de corriente",0,1]
它包含整数和字符串。你期待 Integer
List<List<List<Integer>>> result
这就是它抛出 NumberFormatException
的原因
将所有整数转换为字符串
["12","01","1","Fallo de corriente","0","1"]
并期待 String
这样的结果
List<List<List<String>>> result
我有以下Json
信息,想拿出来Listview
:
{"result":[[[12,"01",1,"Fallo de corriente",0,1],
[12,"01",2,"Nivel m\u00E1ximo (activaci\u00F3n)",0,0],
[12,"01",3,"Nivel m\u00E1ximo(desactivaci\u00F3n)",0,1]]]
}
事实是应用程序给我一个错误,我没有提示,我是这样安装的:
我有POST
class如下:
package com.example.lista.Interface;
import java.util.List;
import java.lang.Integer;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Post {
@SerializedName("result")
@Expose
private List<List<List<Integer>>> result = null;
public List<List<List<Integer>>> getResult() {
return result;
}
public void setResult(List<List<List<Integer>>> result) {
this.result = result;
}
}
PostService 接口:
package com.example.lista.Interface;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
public interface PostService {
String API_ROUTE = " obtenerListaAlarmasSMS";
@GET(API_ROUTE)
Call<Post> getPost(@Header("Authorization") String credencialesEnBase64);
}
我的主要内容如下
package com.example.lista.Interface;
import android.os.Bundle;
import android.util.Log;
import android.util.Base64;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.lista.R;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.lang.String;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import static retrofit2.Response.error;
public class MainActivity extends AppCompatActivity {
ListView list;
List<List<List<List<Integer>>>> titles = new
ArrayList<List<List<List<Integer>>>>();
ArrayAdapter arrayAdapter;
String usuario = new String("");
String clave = new String("");
String error = new String("Algo ha fallado");
String credenciales = usuario + ":" + clave ;;
String credencialesEnBase64 = "Basic " +
Base64.encodeToString(credenciales.getBytes(), Base64.NO_WRAP);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, titles);
list = findViewById(R.id.list);
list.setAdapter(arrayAdapter);
getPosts();
}
private void getPosts() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.249:9096/datasnap/rest/TMetodosREST/")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostService postService = retrofit.create(PostService.class);
Call<Post> call = postService.getPost(credencialesEnBase64);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
for (Post post : response.body()) {
titles.add(post.getResult());
}
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.d("MainActivity", t.getMessage());
}
});
}
}
问题出在它告诉我的 OnResponse 中:
error: for-each not applicable to expression type required: array or java.lang.Iterable found: Post
我不知道是因为创建我的POJO的格式还是它发生了什么。
我想要的是获取ListView
As per your output
[12,"01",1,"Fallo de corriente",0,1]
它包含整数和字符串。你期待 Integer
List<List<List<Integer>>> result
这就是它抛出 NumberFormatException
将所有整数转换为字符串
["12","01","1","Fallo de corriente","0","1"]
并期待 String
这样的结果
List<List<List<String>>> result