使用GSON解析JSON以在Android中填充ListView时出现问题 [英] Problem in Using GSON to parse JSON to populate a ListView in Android

查看:94
本文介绍了使用GSON解析JSON以在Android中填充ListView时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON回复:

My JSON response :

[
{
"IDNumber": 2,
"Name": "Saqib Hyder",
"FatherName": "M Sami Hyder",
"Age": "26",
"DateOfBirth": "16th Nov YYYY",
"Occupation": "Senior .NET Dev",
"MartialStatus": "UnMarried",
"Brand": "XYZ",
"UserStatus": "Family"
},
{
"IDNumber": 3,
"Name": "M Asim Hyder",
"FatherName": "M Sami Hyder",
"Age": "29",
"DateOfBirth": "16th May YYYY",
"Occupation": "Executive Shu Shef",
"MartialStatus": "Married",
"Brand": "XXY",
"UserStatus": "Family"
},
{
"IDNumber": 4,
"Name": " Wali Hyder",
"FatherName": "M Sami Hyder",
"Age": "34",
"DateOfBirth": "29th Nov YYYY",
"Occupation": "Senior Consultant",
"MartialStatus": "Married",
"Brand": "YYZ",
"UserStatus": "Family"
},
{
"IDNumber": 5,
"Name": "Sami Uddin",
"FatherName": "M Uddin",
"Age": "21",
"DateOfBirth": "DD MM 1994",
"Occupation": "Student | Tutor",
"MartialStatus": "UnMarried",
"Brand": "PQR",
"UserStatus": "Friend"
}
]







我的Pojo课程






My Pojo Class

public class PersonData {

	public static String Name;
	static String FatherName;
	static String Age;
	static String Occupation;
	static String MartialStatus;
	static String UserStatus;
	static String Brand;
	static String DateOfBirth;

	@Override
	public String toString() {

		return Name; //I am just returning name here to avoid Custom list thingy , will come to this just need to get Response in first place 
	}

}





我的Java类代码





My Java Class Code

public class MainActivity extends ListActivity {

	Button btnGet;
	ListView list_Data;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btnGet = (Button) findViewById(R.id.btnGET);
		btnGet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ApiCaller objApiCaller = new ApiCaller();
				objApiCaller.execute();

			}
		});
	}
	
	class ApiCaller extends AsyncTask<Object, Object, Object> {


		@Override
		protected void onPostExecute(Object result) {
			
                        String data = result.toString();
			Gson g = new Gson();
			

			Type type = new TypeToken<PersonData[]>() {
			 }.getType();
			
			PersonData[] pd = g.fromJson(data, type);
			list_Data = getListView();
			list_Data.setAdapter(new ArrayAdapter<PersonData>(
					getApplicationContext(),
					android.R.layout.simple_expandable_list_item_1, pd));
		}

		@Override
		protected Object doInBackground(Object... params) {

			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(
					"http://192.168.0.100:7001/com.faisal.REST_WS/api/v1/json");
			ResponseHandler<String> handler = new BasicResponseHandler();
			Object result = new Object();

			try {
				result = client.execute(request, handler);
			} catch (ClientProtocolException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
				
				e.printStackTrace();
			}

			return result;
		}

	}
}









XML代码







XML code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.faisal.api_1.MainActivity" >

    <ListView

        android:id="@android:id/list"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_above="@+id/btnGET" />

    <Button

        android:id="@+id/btnGET"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:text="Get Data"

        tools:ignore="Hardcodedtext" />

</RelativeLayout>









错误:



预计BEGIN_ARRAY但在第1列是STRING .....



我试过谷歌,但不是可以解决:(请帮助。!





Error:

Expected BEGIN_ARRAY but was STRING at column 1.....

I tried google but was not able to solve :( Kindly help.!

推荐答案

protected void onPostExecute(Object result) {
			if (pDialog.isShowing()) {
				pDialog.dismiss();
			}

			String json = result.toString();
			Gson gson = new Gson();
			Type type = new TypeToken<list><persondata>>() {
			}.getType();
			List<persondata> pd = (List<persondata>) gson.fromJson(json, type);
			dataAdapter = new MyCustomAdapter(Main.this, pd);
			listView = getListView();
			listView.setAdapter(dataAdapter);

}


这篇关于使用GSON解析JSON以在Android中填充ListView时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆