我正在开发一个Android应用程序,它可以从Web服务获取gson响应。
但是,出现了这个例外:
com.google.gson.stream.malformed json eception:使用json reader.setlentient(true) 在第1行第9列接受格式错误的json
Web服务将json作为对象数组发送 我不知道出了什么问题。
这是我的代码:
public class Clinci_info_frag extends Fragment{
ListView lvcli;
TextView clinct;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreateView(inflater, container, savedInstanceState);
View v =inflater.inflate(R.layout.clinic_info_frag, container,false);
lvcli=(ListView) v.findViewById(R.id.ClinicinfoList);
clinct = (TextView) v.findViewById(R.id.clinict);
for (int i = 1 ; i<=30 ; i++){
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://lavendergroup.net/web_service/api/getClinicInfo?id="
+ i;
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public void onSuccess(int arg0, String response) {
// TODO Auto-generated method stub
super.onSuccess(arg0, response);
Gson gson = new Gson();
try {
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(response)
.getAsJsonArray();
DataBasenewHelper dbh = new DataBasenewHelper(getActivity());
for (JsonElement obj : jArray) {
Clinic_infoweb ido = gson.fromJson(obj,
Clinic_infoweb.class);
Clinic_info cl = new Clinic_info();
cl.setId(ido.getId());
cl.setName(ido.getName());
cl.setInfo(ido.getInfo());
cl.setFrom_date(ido.getFrom_date());
cl.setTo_date(ido.getTo_date());
dbh.createClinic_info(cl);
ArrayList<Clinic_infoweb> _listFromDataBase=dbh.getAllClinic_info();
ArrayList<String> data=new ArrayList<String>();
for (int i = 0; i < _listFromDataBase.size(); i++) {
Clinic_infoweb ci=_listFromDataBase.get(i);
data.add(ci.getId()+" "+ci.getName()+" "+ci.getFrom_date()+" "+ ci.getTo_date()+" "+ci.getInfo());
Toast.makeText(getActivity(), ci.getId() + ci.getName() + ci.getInfo(), Toast.LENGTH_LONG).show();
}
ArrayAdapter<String> myadapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_expandable_list_item_1,data);
lvcli.setAdapter(myadapter);
lvcli.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("static-access")
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
int itemPosition = position;
String itemValue = (String) lvcli.getItemAtPosition(position);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setMessage(itemValue);
alert.setCancelable(true);
alert.create().show();
}
});
}
} catch (Exception e) {
// Toast.makeText(getActivity(), e.getMessage(),
// Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Throwable arg0, String arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
//Toast.makeText(getActivity(), "فشل تحديث جدول بيانات العيادات",
// Toast.LENGTH_LONG).show();
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
}
});
}
return v;
}
}
有人可以帮我吗?