Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to read data from MySQL in wamp server using android app but I got Data parsing error.I am using genymotion emulator in android studio


C#
public void getData() {
        String result = "";
        InputStream isr = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.0.2/androidDb/read.php"); //YOUR PHP SCRIPT ADDRESS
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
            Log.e("pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
            nameView.setText("Couldnt connect to database");
        }
        //convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(isr,  HTTP.UTF_8), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr.close();
            result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
        //parse json data
        try {
            String s = "";
            JSONArray jarray = new JSONArray(result);
            for(int i=0; i<jarray.length(); i++)
            {
                JSONObject json = jarray.getJSONObject(i);
                s = s + "ID " +json.getInt("ID")+"\nName: "+json.getString("Name")+"\n\n";
            }
            nameView.setText(s);
            } catch (Exception e) {

                Log.e("log_tag", "Error Parsing Data " + e.toString());
            }
}
Posted
Updated 26-Mar-15 6:37am
v2
Comments
Sergey Alexandrovich Kryukov 26-Mar-15 12:09pm    
DOCTYPE?! You input is not JSON, not even close. :-)
—SA
Member 10808323 26-Mar-15 13:10pm    
will you please elleborate your point
Sergey Alexandrovich Kryukov 26-Mar-15 14:58pm    
Well, show your input, at least few first lines, and I'll tell you what is that. I don't think this is JSON data. How else could possibly you understand my comment?
—SA

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