我尝试使用api显示从数据库返回的项目列表,并且在浏览器中工作正常,我也可以返回单个项目而没有任何问题,尝试返回整个列表并将其放入Java中代码给我带来很多问题。我的代码真的搞砸了,我不确定问题出在哪里,但我想是,它无法正确存储返回的值。
MainPageActivity:
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)
答案 0 :(得分:0)
您进入String strJson = convertStreamToString(inputStream);
的响应是JsonArray
,您正在使用Eventi x = MyGson.build().fromJson(strJson, Eventi.class);
将其转换为模型对象
这就是为什么它引发异常IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
答案 1 :(得分:0)
我建议对HTTP请求使用翻新 https://square.github.io/retrofit/