开发者社区> 问答> 正文

在Android Studio中将数据从MySQL数据库显示到TextView中

我是新手,Android Studio我想将数据库(我使用MySQL)中的数据显示到中TextView。我也使用Button和RadioButton。单击按钮后,数据将显示在中TextView。这是我的PHP代码:

<?php 

$host = 'localhost';
$user = 'root';
$pass = 'bismillah';
$database = 'rizka';
$conn = mysqli_connect($host, $user, $pass, $database);
if(!$conn) {
    die('Koneksi database gagal: ');
}else{
     echo 'berhasil<br>';
}

    $dtkategori = $_GET["kategori"];
    $sql = "SELECT * FROM manarul WHERE kategori = '$dtkategori'";
    $hasil = mysqli_query($conn, $sql);

    while($row = mysqli_fetch_row($hasil)) {
        echo "Jumlah Slot = " .$row[1];
        echo "\n";
        echo "adalah " .$row[2];
        echo "\n";

    }
?>

这是我的Java代码:

package com.example.googlemaps;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.util.Log;

public class CobaActivity extends AppCompatActivity {
        public static final String LOG_TAG ="dataBaru";

    String memilih[]={"Silakan Pilih Tempat Parkir", "Masjid Manarul", "Gedung FTSP", "Gedung Rektorat"};
    private RadioGroup radioGroupNb;
    private RadioButton radioButtonKosong, radioButtonTerisi;
    private TextView txt;
    private Button pilih, proses;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coba);
        txt = (TextView) findViewById(R.id.TextView03);
        pilih = (Button) findViewById(R.id.pilih_parkir1);
        proses = (Button) findViewById(R.id.tampil_slot);
        radioGroupNb = (RadioGroup) findViewById(R.id.radioGroupNb);
        radioButtonKosong = (RadioButton) findViewById(R.id.radioButtonKosong);
        radioButtonTerisi = (RadioButton) findViewById(R.id.radioButtonTerisi);
        proses.setOnClickListener(new clickproses());

        getSupportActionBar().setTitle("Pilih Gedung Parkir");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, memilih);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                if (position==1){
                    final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
                    pilih.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(CobaActivity.this, Pilihan_Parkir_Lanjut.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


    }
    class clickproses implements Button.OnClickListener {
        public void onClick (View view){

            String dtkategori = null;
            if (radioButtonKosong.isChecked()){
                dtkategori = "kosong";
            }
            if (radioButtonTerisi.isChecked()){
                dtkategori = "terisi";
            }
            String str = downloadTeks("http://192.168.1.9/cobaget.php?kategori="+dtkategori);
            txt.setText(str);
        }
    }

    private InputStream OpenHttpConnection (String urlString) throws IOException {
        InputStream in = null;
        int response = -1;
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private String downloadTeks (String URL) {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
        } catch (IOException e){}
                int charRead;
                StringBuffer data = new StringBuffer();
                String str = "";
        try {
                while ((charRead = in.read()) != -1){
                    data.append((char) charRead);
                }
                str = data.toString();
                in.close();;
        } catch (IOException e){}
        return str;
        }
}

这是我的数据库: MySQL数据库名称是Rizka,表名称是manarul

展开
收起
垚tutu 2019-12-26 18:20:09 1975 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载