Listview in Android while getting data from a server
I am trying to populate the data into listview Asynchronously
I am retrieving data from server as JSON response
MainActivity.java
public class MainActivity extends Activity {
// url to make request
private static String url="http://54.218.73.244:7002/";
ListView yourListView;
List<Item> yourData = new ArrayList<Item>();
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourListView = (ListView) findViewById(R.id.listViewID);
//Instantiating ProgressDialog with onCreate method
progressDialog=new ProgressDialog(MainActivity.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog=ProgressDialog.show(MainActivity.this, "",
"Please Wait", true, false);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
//Create a JSON parser Instance ----- Used JSON parser from
Android
JSONObjParser jParser=new JSONObjParser();
//Getting JSON string from URL ------ Used JSON Array from
Android
JSONArray json=jParser.getJSONFromUrl(url);
List<Item> yourData = new ArrayList<Item>();
try {
for(int i=0;i<json.length();i++)
{
JSONObject c=json.getJSONObject(i);// Used JSON Object
from Android
String RESTAURANT_NAME=c.getString("restaurantNAME");
yourData.add(new Item(RESTAURANT_NAME));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
ListAdapter customAdapter = new ListAdapter(MainActivity.this,
R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
}
}
}
JSONParser.java
public class JSONParser {
static InputStream is = null;
static JSONArray jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
Log.i("test", "hello1");
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.i("test", "hello");
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
Log.i("test", "4");
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
Log.i("test", "3");
StringBuilder sb = new StringBuilder();
Log.i("test", "2");
String line = null;
Log.i("test", "1");
while ((line = reader.readLine()) != null) {
Log.i("test", "line");
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
Log.i("test", "afterline");
// try parse the string to a JSON object
try {
JSONObject jsonObject = new JSONObject(json);
Log.i("parsing..", ""+jsonObject.get("student").toString());
jObj = new JSONArray(jsonObject.get("student").toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
log::
08-23 20:01:53.746: E/JSON Parser(1130): Error parsing data
org.json.JSONException: Value Cannot of type java.lang.String cannot be
converted to JSONObject
08-23 20:01:53.770: W/dalvikvm(1130): threadid=9: thread exiting with
uncaught exception (group=0x40015560)
08-23 20:01:53.780: E/AndroidRuntime(1130): FATAL EXCEPTION: AsyncTask #1
08-23 20:01:53.780: E/AndroidRuntime(1130): java.lang.RuntimeException: An
error occured while executing doInBackground()
08-23 20:01:53.780: E/AndroidRuntime(1130): at
android.os.AsyncTask$3.done(AsyncTask.java:200)
08-23 20:01:53.780: E/AndroidRuntime(1130): at
java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
JSON :: http://54.218.73.244:7002/
{
"restaurants": [
{
"restaurantID": 1,
"restaurantNAME": "CopperChimney"
},
{
"restaurantID": 2,
"restaurantNAME": "Aroy"
},
{
"restaurantID": 3,
"restaurantNAME": "MarkBoulevard"
},
{
"restaurantID": 4,
"restaurantNAME": "Indian"
}
],
"RestaurantTimings": [
{
"_id": 1,
"RestaurantTime": "8pm to 11pm"
},
{
"_id": 2,
"RestaurantTime": "10pm to 12pm"
},
{
"_id": 3,
"RestaurantTime": "11pm to 9pm"
},
{
"_id": 4,
"RestaurantTime": "10pm to 5pm"
}
]
}
Any Ideas
Thanks
No comments:
Post a Comment