i am not getting Json response in my listactivity.i tried everything but its not showing the result..in logcat i got successful connection and data also i can see..but in my listview its says this exception. here is my code
public class TypeMenu extends AppCompatActivity {
private String TAG = TypeMenu.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = "http://cloud.granddubai.com/brtemp/index.php";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_menu);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(TypeMenu.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
Toast.makeText(getApplicationContext(),
"Toast",
Toast.LENGTH_LONG)
.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("menu_type");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String type = c.getString("type");
//String email = c.getString("email");
// String address = c.getString("address");
// String gender = c.getString("gender");
// Phone node is JSON Object
//JSONObject phone = c.getJSONObject("phone");
//String mobile = phone.getString("mobile");
// String home = phone.getString("home");
// String office = phone.getString("office");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", type);
//contact.put("email", email);
//contact.put("mobile", mobile);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
TypeMenu.this, contactList,
R.layout.list_item, new String[]{"id", "type"}, new int[] {R.id.id,
R.id.type});
lv.setAdapter(adapter);
}
}
}
here is my logcat msg
12-15 14:25:26.092 549-962/com.example.zass.broccoli E/TypeMenu: Response from url: connection successful[{"id":"1","type":"pizza"}, {"id":"8","type":"Special Offer"},{"id":"2","type":"Pasta"},{"id":"7","type":"Soup"},{"id":"6","type":"Beverages"},{"id":"5","type":"Breakfast"},{"id":"3","type":"Lasagna"},{"id":"4","type":"Salad"}] 12-15 14:25:26.092 549-962/com.example.zass.broccoli E/TypeMenu: Json parsing error: Value connection of type java.lang.String cannot be converted to JSONObject
this is the output from url which i wanted in my listview
[{"id":"1","type":"pizza"},{"id":"8","type":"Special Offer"}, {"id":"6","type":"Beverages"},{"id":"5","type":"Breakfast"}, {"id":"3","type":"Lasagna"},{"id":"4","type":"Salad"}]
here is my json file which works fine on server
<?php
include ('config.php');
$id = $_GET['id'];
$sql = mysqli_query($conn,"SELECT * FROM menu_type ");
$i=0;
while($result = mysqli_fetch_array($sql))
{
$arr[$i]['id']= $result['id'];
$arr[$i]['type']= $result['type'];
}
echo json_encode($arr);
?>
here is my list_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Apply this code
JSONArray jsonArry = new JSONArray(jsonStr);
for (int i = 0; i < jsonArry.length(); i++) {
JSONObject c = jsonArry.getJSONObject(i);
String id = c.getString("id");
String type = c.getString("type");
HashMap<String, String> contact = new HashMap<>();
contact.put("id", id);
contact.put("name", type);
contactList.add(contact);
}
Change below code
ListAdapter adapter = new SimpleAdapter(
TypeMenu.this, contactList,
R.layout.list_item, new String[]{"id", "type"}, new int[] {R.id.id,
R.id.type});
with
ListAdapter adapter = new SimpleAdapter(
TypeMenu.this, contactList,
R.layout.list_item, new String[]{"id", "name"}, new int[] {R.id.id,
R.id.type});