我尝试使用 API 显示从数据库返回的项目列表,它在浏览器中运行良好,我也可以毫无问题地返回单个项目,尝试返回整个列表并将其放入 Java 代码中会给我带来很多问题。我的代码真的很困惑,我不确定问题出在哪里,但我的猜测是,它无法正确存储返回的值。
主页 Activity :
public class MainPageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
final ListView listView = (ListView) findViewById(R.id.ListListaEvenata);
if(EventiLista.getListaEvenata()==null) {
postaviEvente();
}
listView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
if(EventiLista.getListaEvenata()!=null)
return EventiLista.getListaEvenata().size();
else return 0;
}
@Override
public Object getItem(int position) {
return EventiLista.getListaEvenata().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Eventi x = EventiLista.getListaEvenata().get(position);
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.eventistavka, parent, false);
TextView Naziv = (TextView) view.findViewById(R.id.EventNaziv);
TextView Opis = (TextView) view.findViewById(R.id.OpisEventa);
Naziv.setText(x.Naziv);
Opis.setText(x.Opis);
return null;
}
});
}
protected void postaviEvente() {
EventiApi.GetEvente(this, new MyRunnable<List<Eventi>>() {
@Override
public void run(List<Eventi> result) {
EventiLista.setListaEvenata(result);
}
});
}
这也是我的 EventiApi 类,可能是导致问题的类:
public class EventiApi {
public static List<Eventi> listEventi = EventiLista.getListaEvenata();
public static List<Eventi> GetEvente(final Context context, final MyRunnable<List<Eventi>> onSuccess) {
if(listEventi==null) {
new AsyncTask<Void, Void, List<Eventi>>() {
@Override
protected List<Eventi> doInBackground(Void... voids) {
HttpGet httpGet = new HttpGet(Config.url + "api/Eventi");
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(httpGet);
InputStream inputStream = response.getEntity().getContent();
String strJson = convertStreamToString(inputStream);
Eventi x = MyGson.build().fromJson(strJson, Eventi.class);
listEventi.add(x);
} catch (IOException e) {
Log.e("HttpManager", e.getMessage());
EventiLista.setListaEvenata(null);
}
return listEventi;
}
protected void onPostExecute(List<Eventi> rezultat) {
if (rezultat == null)
Toast.makeText(MyApp.getContext(), "Greska u komunikaciji sa serverom", Toast.LENGTH_SHORT).show();
else {
onSuccess.run(rezultat);
}
}
}.execute();
}
return listEventi;
}
public static String convertStreamToString(InputStream inputStream) throws IOException {
String newLine = System.getProperty("line.separator");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
boolean flag = false;
for (String line; (line = reader.readLine()) != null; ) {
result.append(flag ? newLine : "").append(line);
flag = true;
}
return result.toString();
}
}
这也是我的 MyRunnable 界面:
public interface MyRunnable<T> {
void run(T result);
}
还有我的 EventiLista 辅助类:
public class EventiLista {
private static List<Eventi> listaEvenata;
public static List<Eventi> getListaEvenata() {
return listaEvenata;
}
public static void setListaEvenata(List<Eventi> listaEvenata) {
EventiLista.listaEvenata = listaEvenata;
}
}
MyGson:
public class MyGson {
public static Gson build()
{
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
return builder.create();
}
}
非常感谢任何帮助或想法,感谢大家的宝贵时间!
它应该在我的列表中显示返回的值,但应用程序只是中断了。
这是 logcat 的一部分:
Process: com.example.evente, PID: 6151
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:888)
at com.google.gson.Gson.fromJson(Gson.java:853)
at com.google.gson.Gson.fromJson(Gson.java:802)
at com.google.gson.Gson.fromJson(Gson.java:774)
at com.example.evente.api.EventiApi$1.doInBackground(EventiApi.java:46)
at com.example.evente.api.EventiApi$1.doInBackground(EventiApi.java:29)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
at com.google.gson.Gson.fromJson(Gson.java:888)
at com.google.gson.Gson.fromJson(Gson.java:853)
at com.google.gson.Gson.fromJson(Gson.java:802)
at com.google.gson.Gson.fromJson(Gson.java:774)
at com.example.evente.api.EventiApi$1.doInBackground(EventiApi.java:46)
at com.example.evente.api.EventiApi$1.doInBackground(EventiApi.java:29)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
最佳答案
您在 String strJson = ConvertStreamToString(inputStream);
中得到的响应是 JsonArray
,您正在使用 Eventi x = MyGson.build().fromJson(strJson, Eventi.class);
这就是它抛出异常的原因IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
关于java - 无法显示 api 返回项目的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57414357/