3

I am creating a project in android studio. I am getting this error while retrieving information from a sql server database. I am using .php files for database connection. On debugging I found that the following line is generating the error :

inputStream = urlConnection.getInputStream();

please see my code

 private class ConnectToDatabaseMain extends AsyncTask<String, Void, Patient[]>
{
   .....

    protected XXXX[] doInBackground(String... params) {
        // if (params.length == 0) return null; // Nothing to look up

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        InputStream inputStream;
        StringBuffer buffer = new StringBuffer();
        JSONObject jsonObject = new JSONObject();
        String jsonString;
        String serverUrl = params[0];
        String date = params[1];

        try {
            // Create the request to the network database


            URL url = new URL(serverUrl);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.connect();


            jsonObject.put("date", date);




            DataOutputStream printout = new DataOutputStream(urlConnection.getOutputStream());
            printout.writeBytes(jsonObject.toString());
            printout.flush();
            printout.close();

            // Read the input stream into a String
            inputStream = urlConnection.getInputStream();
            if (inputStream == null) return null; 
Sandesh
  • 1,190
  • 3
  • 23
  • 41
Tirthadip
  • 41
  • 2
  • 3
  • check if your internet is connected or not while performing this operation – AbhayBohra Sep 28 '16 at 09:43
  • On further inspection I have found that if I access my PHP files(which are used to query sql-server) over **https://** then the project is running well........but whenever I am accessing my php files over **http://**, the IOException is raised. – Tirthadip Oct 04 '16 at 04:06
  • https://stackoverflow.com/a/49485240/8311441 – Tabish Mar 26 '18 at 06:31

0 Answers0