Answer the question
In order to leave comments, you need to log in
How to set up a POST method from Java to a php script?
Goal: I need to send the id field from AndroidStudio to a php script, and then, based on the id, return the desired database string. Created a class for connecting to the database, if you do not use id, then the information is returned. I think I'm somehow writing the POST
Class for the connection wrong:
package com.example.maybe;
import android.content.Context;
import android.os.AsyncTask;
import android.text.Html;
import android.widget.TextView;
import androidx.loader.content.AsyncTaskLoader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class GetData extends AsyncTask<String, Void, String > {
private Context context;
private TextView text;
private int id = 1;
public GetData(Context context, TextView text, int day){
this.context= context;
this.text = text;
this.id = day;
}
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0){
try {
String link ="https://bdforandroid.000webhostapp.com/getBD.php";
String data = URLEncoder.encode("id", "UTF-8") + "-" +id;
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line);
break;
}
return sb.toString();
} catch (Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
this.text.setText(Html.fromHtml(result));
}
}
$con=mysqli_connect("localhost", "id16937257_cheta", "d0VALqS!8%q~kYXh", "id16937257_bdforandroid");
$id = $_POST['id'];
$result = mysqli_query($con,"SELECT `date` FROM `holidays` WHERE `id` = $id");
$row = mysqli_fetch_array($result);
$data = $row[0];
echo $data;
mysqli_close($con);
Answer the question
In order to leave comments, you need to log in
Good afternoon.
i need to send id field from androidstudio
Created a class for connecting to the db,
String link ="https://bdforandroid.000webhostapp.com/getBD.php";
String data = URLEncoder.encode("id", "UTF-8") + "-" +id;
if you do not use id, then the information is returned
$id = $_POST['id'];
$result = mysqli_query($con,"SELECT `date` FROM `holidays` WHERE `id` = $id");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question