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();
}
}
}