C
C
che_ta2021-06-01 00:50:21
PHP
che_ta, 2021-06-01 00:50:21

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


php script:
$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

1 answer(s)
O
Orkhan, 2021-06-01
Hasanly @azerphoenix

Good afternoon.

i need to send id field from androidstudio

Well, first of all, Android Studio is just an editor. And you will send a request, for example, from a mobile application.
Secondly, well, you can, for example, look at this answer - https://stackoverflow.com/questions/2938502/sendin...
It says how to make a POST request. You can also look towards using the OkHttp or Retrofit libraries.
Created a class for connecting to the db,

Rather, you are making a REST request rather than connecting to a database. To connect to the database, you need to use jdbc, which is not necessary in your case. It is enough to make a POST request to the REST service.
I'm guessing you might be making a mistake here:
String link ="https://bdforandroid.000webhostapp.com/getBD.php";
            String data = URLEncoder.encode("id", "UTF-8") + "-" +id;

For example, dash is superfluous before id
if you do not use id, then the information is returned

And here, a certain id for the input is clearly expected
$id = $_POST['id'];
    $result = mysqli_query($con,"SELECT `date` FROM `holidays` WHERE `id` = $id");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question