Error in getting data from MySQL&PHP and JSON

  • Replies:1
slsv
  • Forum posts: 1

Oct 29, 2014, 12:53:04 PM via Website

Hello,
I'm kinda new to android and have some problems that I don't know how to fix.

So I have MainActivity.java where I've defined button which when is clicked open second Activity. The second activity must retrieve data from mysql but I get that error instead:

Error org.json.JSONEXception: Value of type java.lang.String cannot be converted to JSONObject

here is the code of second activity:

public class Restaurants extends Activity {
private String jsonResult;
private String url = "link to my localhost";
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurants);
listView = (ListView) findViewById(R.id.listView1);
accessWebService();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost(params[0]);
  try {
        HttpResponse response = httpclient.execute(httppost);
        jsonResult = inputStreamToString(
        response.getEntity().getContent()).toString();
  }

  catch (ClientProtocolException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
 }
 return null;
 }

private StringBuilder inputStreamToString(InputStream is) {
 String rLine = "";
 StringBuilder answer = new StringBuilder();
 BufferedReader rd = new BufferedReader(new InputStreamReader(is));

 try {
   while ((rLine = rd.readLine()) != null) {
   answer.append(rLine);
  }
}

  catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
  "Error..." + e.toString(), Toast.LENGTH_LONG).show();
 }
  return answer;
 }

@Override
protected void onPostExecute(String result) {
 ListDrwaer();
}
}// end async task

public void accessWebService() {
JsonReadTask task = new JsonReadTask();
 // passes values for the urls string array
task.execute(new String[] { url });
}

// build hash set for list view
public void ListDrwaer() {
 List<Map<String, String>> restaurantList = new ArrayList<Map<String, String>>();

try {
  JSONObject jsonResponse = new JSONObject(jsonResult);
  JSONArray jsonMainNode = jsonResponse.optJSONArray("restoranti");

 for (int i = 0; i < jsonMainNode.length(); i++) {
 JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
 String name = jsonChildNode.optString("name");
 String menu = jsonChildNode.optString("menu");
 String outPut = name + "-" + menu;
 restaurantList.add(createRestaurants("restoranti", outPut));
}
} catch (JSONException e) {
 Toast.makeText(getApplicationContext(), "Error " + e.toString(),
   Toast.LENGTH_SHORT).show();
}

 SimpleAdapter simpleAdapter = new SimpleAdapter(this, restaurantList,
android.R.layout.simple_list_item_1,
new String[] { "restoranti" }, new int[] { android.R.id.text1 });
 listView.setAdapter(simpleAdapter);

}

 private HashMap<String, String> createRestaurants(String name, String menu) {
 HashMap<String, String> restaurantNameNo = new HashMap<String, String>();
 restaurantNameNo.put(name, menu);
 return restaurantNameNo;
  }
 }

— modified on Oct 29, 2014, 1:55:49 PM

Reply
Kevin Berendsen
  • Forum posts: 118

Nov 7, 2014, 11:38:01 AM via Website

Could you show me the result of the JSON query that gets stored in the string (jsonResult)? I think the result contains some incorrect characters or is missing some.

Reply