1

At present I am retrieving data via an external URL which engages a PHP script and returns JSON values. It works fine.

But how do I send a String to be used for query so that I can do the following at the PHP script:

$name = $_POST['name']; //this was sent from the Android app. How do I send this from the app.
$sql = "SELECT name FROM tablename WHERE category = '$name'";
$result = mysqli_query($con, $sql) or die("Error in Selecting");

Note: I do not want to use Volley.

My AsyncTask extended class which currently doesn't send any value over to the URL.

class BackgroundTask extends AsyncTask<Void,Void,String> {

        final String jsonUrl = "http://example.com/getdata.php";

        @Override
        protected String doInBackground(Void... params) {
            try {
                URL url = new URL(jsonUrl);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                InputStream in = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                StringBuilder stringBuilder = new StringBuilder();

                while ((jsonString = bufferedReader.readLine()) != null){
                    stringBuilder.append(jsonString+"\n");
                }

                bufferedReader.close();
                in.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();

            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            products = new ArrayList<>();

            try {
                jsonObject = new JSONObject(s);
                String category = jsonObject.getString("viewProduct");
                JSONArray jsonArray = new JSONArray(category);

                for(int x=0; x < jsonArray.length(); x++){
                    JSONObject jsonPart = jsonArray.getJSONObject(x);
                    products.add(jsonPart.getString("name"));
                }

                adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, products);
                productListView.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
JasSy
  • 583
  • 5
  • 17

1 Answers1

0

If you're just talking about sending data as the body of the POST request, you can do it with something like this:

HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
conn.setDoOutput( true );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
try {
   DataOutputStream wr = new DataOutputStream( conn.getOutputStream());
   wr.write( postData );
} catch (IOException ex) {}
John Leehey
  • 22,052
  • 8
  • 61
  • 88
  • I presume this occurs inside doInBackground method? – JasSy Jun 15 '16 at 16:10
  • Yup, it involves synchronous network transfer so you'll definitely want it off the main thread. – John Leehey Jun 15 '16 at 16:13
  • How do I send out the String in this case. wr.write method signatures only taking in int and byte arrays for that postData value. – JasSy Jun 15 '16 at 16:24
  • Once you have the stream, you can write data to it the same way you write to any stream. Take a look at a PrintWriter, something like this answer: http://stackoverflow.com/questions/4069028/write-string-to-output-stream – John Leehey Jun 16 '16 at 17:35