Marco Ripamonti
Marco Ripamonti

Reputation: 99

Remote connection to a mysql database with android php json?

I'm trying to connect toa mysql database with an android application with an HTTP connection and then retriving a JSONArray to scan results from the query. Here is the code:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        new JSONfunction().execute();
    }

    private class JSONfunction extends AsyncTask<Void,Void,JSONArray> {

        private JSONArray getJSONfromURL(String url) {
            InputStream is = null;
            String result = "";
            JSONArray jArray = null;


            // Download JSON data from URL
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }

            // Convert response to string
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
                jArray=new JSONArray(result);
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }


            return jArray;
        }



        @Override
        protected JSONArray doInBackground(Void... voids) {
            JSONArray jArray=getJSONfromURL("http://192.168.1.5/ConnectData/index.php");
            return jArray;
        }

        protected void onPostExecute(JSONArray result) {
            TextView prova = (TextView)findViewById(R.id.txtProva);
            try {

                JSONObject rec = result.getJSONObject(1);
                prova.setText(rec.getString("first_name"));

            }
            catch(JSONException e){
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
        }
    }

I'm getting an error when it is executing the while. After 2 or 3 cycles of the while the application crashes. Can you give me any help?

[edit]

I'm getting this error: 11-28 20:42:19.006 27688-27702/com.example.marco.provadatabase E/log_tag﹕ Error converting result org.json.JSONException: Value !DOCTYPE of type java.lang.String cannot be converted to JSONArray

[edit 2]

Ok I understood the problem. BUt now I'm facing a new one. Today a tried again to execute the code but now I am getting a time out exception on thi line HttpResponse response = httpclient.execute(httppost); (It worked fine yesterday and I didn't change anything). It isn't a URL problem because if I put it on a browser it works fine.

Upvotes: 0

Views: 397

Answers (1)

Khashayar Ghamati
Khashayar Ghamati

Reputation: 368

your response type is String you should convert it to JSONObject

Upvotes: 0

Related Questions