Click here to Skip to main content
14,830,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My JSON response :
Java
[
{
"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"
}
]



My Pojo Class

Java
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 
	}

}


My Java Class Code

Java
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 code

XML
<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>




Error:

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

I tried google but was not able to solve :( Kindly help.!
Posted
Updated 15-Jan-15 4:47am
v2

1 solution

Java
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);

}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900