我尝试了两天没有成功,如果有人能在这里帮助我......
我的获取/设置:
public class HTTPResult {
private String id;
private String nome;
private String email;
private String titulo;
private String resumo;
private String texto;
private String fonte;
private String link;
public String getEmail() {
return email;
}
public void setEmail(String email) {
email = email;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
nome = nome;
}
public String getID() {
return id;
}
public void setID(String ID) {
this.id = ID;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getResumo() {
return resumo;
}
public void setResumo(String resumo) {
this.resumo = resumo;
}
public String getTexto() {
return texto;
}
public void setTexto(String texto) {
this.texto = texto;
}
public String getFonte() {
return fonte;
}
public void setFonte(String fonte) {
this.fonte = fonte;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@Override
public String toString(){
return "Titulo: "+ getTitulo()
+ "nResumo: " + getResumo()
+ "nTexto: " + getTexto()
+ "nFonte: " + getFonte()
+ "nLink: " + getLink()
+ "nNome: " + getNome()
+ "nEmail: " + getEmail()
+ "nID: " + getID();
}
}
消费者 JSON WebServ,从 SQL 返回 JsonArray
"SELECT COUNT(*) FROM PERSONS":
public class HTTPService extends AsyncTask<Void, Void, HTTPResult> {
private final String command;
public HTTPService(String command) {
this.command = command;
}
@Override
protected HTTPResult doInBackground(Void... voids) {
StringBuilder resposta = new StringBuilder();
try {
URL url = new URL("http://10.0.2.2:8080/rest-FAKE/rest/fake" + command);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setConnectTimeout(5000);
connection.connect();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()){
resposta.append(scanner.next());
System.out.println(resposta);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new Gson().fromJson(resposta.toString(), HTTPResult.class);
}
}
来自 Adaptador Class (ListView( 的调用方:
@Override
public int getCount() {
String command = "/qnts";
HTTPService service = new HTTPService(command);
int valor = 0;
try {
HTTPResult retorno = service.execute().get();
valor = Integer.parseInt(contagem);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return valor;
}
还有我亲爱的问题:
2020-06-07 22:21:13.456 2825-2888/com.example.fakenewsbrasil I/System.out: [{"id":2}]
2020-06-07 22:21:13.482 2825-2825/com.example.fakenewsbrasil W/System.err: java.util.concurrent.ExecutionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at java.util.concurrent.FutureTask.report(FutureTask.java:123)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at java.util.concurrent.FutureTask.get(FutureTask.java:193)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at android.os.AsyncTask.get(AsyncTask.java:542)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at com.example.fakenewsbrasil.Adaptador.getCount(Adaptador.java:39)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at android.widget.ListView.setAdapter(ListView.java:575)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at com.example.fakenewsbrasil.MainActivity.onCreate(MainActivity.java:57)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.Activity.performCreate(Activity.java:7136)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.Activity.performCreate(Activity.java:7127)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
2020-06-07 22:21:13.483 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at android.os.Looper.loop(Looper.java:193)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6669)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
2020-06-07 22:21:13.484 2825-2825/com.example.fakenewsbrasil W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at com.google.gson.Gson.fromJson(Gson.java:927)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at com.google.gson.Gson.fromJson(Gson.java:892)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at com.google.gson.Gson.fromJson(Gson.java:841)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at com.google.gson.Gson.fromJson(Gson.java:813)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at com.example.fakenewsbrasil.HTTPService.doInBackground(HTTPService.java:45)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at com.example.fakenewsbrasil.HTTPService.doInBackground(HTTPService.java:13)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-06-07 22:21:13.485 2825-2825/com.example.fakenewsbrasil W/System.err: at java.lang.Thread.run(Thread.java:764)
2020-06-07 22:21:13.486 2825-2825/com.example.fakenewsbrasil W/System.err: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
2020-06-07 22:21:13.486 2825-2825/com.example.fakenewsbrasil W/System.err: at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
2020-06-07 22:21:13.486 2825-2825/com.example.fakenewsbrasil W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
2020-06-07 22:21:13.486 2825-2825/com.example.fakenewsbrasil W/System.err: ... 12 more
2020-06-07 22:21:13.489 2825-2888/com.example.fakenewsbrasil E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.fakenewsbrasil, PID: 2825
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
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:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
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:226)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.example.fakenewsbrasil.HTTPService.doInBackground(HTTPService.java:45)
at com.example.fakenewsbrasil.HTTPService.doInBackground(HTTPService.java:13)
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:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
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:215)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.example.fakenewsbrasil.HTTPService.doInBackground(HTTPService.java:45)
at com.example.fakenewsbrasil.HTTPService.doInBackground(HTTPService.java:13)
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:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
2020-06-07 22:21:13.507 2825-2888/com.example.fakenewsbrasil I/Process: Sending signal. PID: 2825 SIG: 9
帮助 t-t
您的响应包含数组,这意味着您应该将其解析为 List。将AsyncTask
更改为此
AsyncTask<Void, Void, ArrayList<HTTPResult>>
Type type = new TypeToken<ArrayList<HTTPResult>>() {}.getType();
return new Gson().fromJson(resposta.toString(), type);
当您提供用于反序列化的类时,默认情况下它将是一个对象。但是在您的场景中,您将获得一个对象数组,一切都很好,只是应该更新 AsyncTask 的返回类型。
您已经使用了AsyncTask<Void, Void, HTTPResult>
public class HTTPService extends AsyncTask<Void, Void, HTTPResult> {
这意味着从AsyncTask
中您将返回一个类型HTTPResult
的对象,但是从后端开始,您有一个对象数组,因此类型应该List<HTTPResult>
。
对HTTPService AsyncTask class
进行以下更改
public class HTTPService extends AsyncTask<Void, Void, List<HTTPResult>> {
...
//rest of your code
Type listType = new TypeToken<List<HTTPResult>() {}.getType();
return new Gson().fromJson(resposta.toString(), listType)
}