我正在解析Json并使用gson库在列表视图上显示。当我插入输入流时,我得到空值。你能告诉我哪里错了我会给你一些步骤 我下载了2.3 Gson库。然后我做了getter和setter
------------------
package com.firstgroup.webservice;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.firstgroup.dto.Holder;
import com.firstgroup.webservicecallback.WebserviceCallBack;
import com.google.gson.Gson;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
public class RequestTask extends AsyncTask<String, String, String>{
private WebserviceCallBack callBack;
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressDialog= new ProgressDialog((Context) callBack);
super.onPreExecute();
progressDialog.setTitle("Please Wait...");
progressDialog.setMessage("Webservice Call...");
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
// I am getting correct result here ...
Reader reader = new InputStreamReader(response.getEntity().getContent());
Gson gson = new Gson();
Holder response1 = gson.fromJson(reader, Holder.class);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..\
progressDialog.hide();
} catch (IOException e) {
//TODO Handle problems..
progressDialog.hide();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
if(callBack!=null){
callBack.getWebserviceResponse(result);
}
progressDialog.hide();
}
public void setObserver(WebserviceCallBack callback){
callBack=callback;
}
}
callback on main activity:
@Override
public void getWebserviceResponse(String response) {
// TODO Auto-generated method stub
Log.d("response", response);
//can I used this code ?
// I want to used gson in main activity?
Reader reader = new InputStreamReader(response.getEntity().getContent());
Gson gson = new Gson();
Holder response1 = gson.fromJson(reader, Holder.class);
}
}
答案 0 :(得分:1)
添加课程:
public class Holder {
List<deparaturedaseboarddto> data;
}
并更改以下代码:
deparaturedaseboarddto response1 = gson.fromJson(reader, deparaturedaseboarddto.class);
为:
Holder response1 = gson.fromJson(reader, Holder.class);
原因:您的json文本值有一个名为data
的根对象,其列表为deparaturedaseboarddto
。您正在尝试将此json值反序列化为deparaturedaseboarddto实例,但它是一个具有deparaturedaseboarddto
列表的对象。
另外(与您的错误无关);
1)类名以大写字母开头,字段名称是Java的驼峰。
2)如果java类的字段名称,您不必使用@SerializedName
与json值的字段名称相同。
3)下面的映射可能会阻止错误,因为json值中没有名为Result
的字段。 @SerializedName(&#34; Result&#34;)可以删除或替换为@SerializedName(&#34; alertsId&#34;)
@SerializedName("Result")
int alertsId;
4)也可能想要替换下面的代码:
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
使用:
responseString = gson.toJson(response1);
在评论中编辑第二个问题: 如果您想使用AsyncTask中的retrieve Holder实例,请进行以下编辑。
改变这个:
public class RequestTask extends AsyncTask<String, String, String>
用这个:
public class RequestTask extends AsyncTask<String, String, Holder>
和:
protected String doInBackground(String... uri)
用这个:
protected Holder doInBackground(String... uri)
和此:
protected void onPostExecute(String result)
用这个:
protected void onPostExecute(Holder result)