package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class MainActivity extends AppCompatActivity {
EditText editText1;
EditText editText2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);
textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = editText1.getText().toString();
String name = editText2.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
RequestData(id, name);
}
}).start();
}
});
}
public void RequestData(String id, String name) {
try {
BufferedReader bufferedReader;
StringBuilder stringBuilder = new StringBuilder();
String urlString = "http://192.168.171.200/data_check.php";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn != null) {
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
String parameter = "id=" + id + "&name=" + name;
OutputStream outputStream = conn.getOutputStream();
outputStream.write(parameter.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
bufferedReader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
textView.setText(stringBuilder);
bufferedReader.close();
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}