php - 在 Android 应用程序中获取并显示单个用户的 MySQL 数据

标签 php android mysql

我正在开发一个 Android 应用程序,我将数据保存在 MySQL 数据库中,然后必须在 Android 应用程序的 TextView 中显示它。

当用户单击“信息”按钮时,他必须查看自己的所有信息。

问题是我不明白该怎么做,我尝试使用本教程的代码“https://androidjson.com/android-php-insert-display-select-update-delete/”,但问题是我没有 View 列表,我只有一键显示,我不明白如何显示单个用户的数据。

Android 应用程序什么也不显示,数据没有到达。

我还尝试在上一个 Activity 中传递用户的 ID,但随后我不知道如何在下一个 Activity 中显示该用户的数据。

我在 MySQL 中的表名为 Restaurants,有 11 个字段。

  • idR
  • 本地名称
  • 中奖
  • 国家
  • 地区
  • 省份
  • 帽子
  • 地址
  • 电子邮件
  • 电话号码

我的 php 代码是:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

include 'config.php';

 $idR= r285602265407430;

// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);

if ($conn->connect_error) {

 die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM Ristoranti WHERE idR = '$idR'" ;

$result = $conn->query($sql);

if ($result->num_rows >0) {


 while($row[] = $result->fetch_assoc()) {

 $tem = $row;

 $json = json_encode($tem);

 echo 'culo';

 }

} else {
 echo "No Results Found.";
}
 echo $json;

$conn->close();
}
?>

我的Java代码是:

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class Modifica_info_base extends AppCompatActivity {

    HttpParse_info httpParse = new HttpParse_info();
    ProgressDialog pDialog;


    // Http Url For Filter Student Data from Id Sent from previous activity.
    String HttpURL = "http://provaord.altervista.org/OrdPlace/InformazioniRistorante/filter_info_base.php";

    String finalResult ;
    HashMap<String,String> hashMap = new HashMap<>();
    String ParseResult ;
    HashMap<String,String> ResultHash = new HashMap<>();
    String FinalJSonObject ;
    String text;
    TextView tNomeRistorante, tPrezzoMedio, tNazione, tRegione, tCitta, tProvincia, tCap, tIndirizzo, tEmail, tNumero;
    String Nomeristorante, Prezzomedio, Nazione, Regione, Citta, Provincia, Cap, Indirizzo, Email, Numero;
    Button UpdateButton;
    String TempItem;
    ProgressDialog progressDialog2;
    EditText idR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modifica_info_base);

        tNomeRistorante = (TextView)findViewById(R.id.textView_nome_ristorante);
        tPrezzoMedio = (TextView)findViewById(R.id.textView_prezzo_medio);
        tNazione = (TextView)findViewById(R.id.textView_nazione);
        tRegione = (TextView)findViewById(R.id.textView_regione);
        tCitta = (TextView)findViewById(R.id.textView_citta);
        tProvincia = (TextView)findViewById(R.id.textView_provincia);
        tCap = (TextView)findViewById(R.id.textView_cap);
        tIndirizzo = (TextView)findViewById(R.id.textView_indirizzo);
        tEmail = (TextView)findViewById(R.id.textView_email);
        tNumero = (TextView)findViewById(R.id.textView_numero_telefono);

        idR = (EditText)findViewById(R.id.editText5id);

        UpdateButton = (Button)findViewById(R.id.button_modifica_info_base);

        TempItem = getIntent().getStringExtra("ListViewValue");

        final String text= idR.getEditableText().toString();

        //Calling method to filter Student Record and open selected record.
        HttpWebCall(text);

        UpdateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Modifica_info_base.this,Messagge_ristoratore.class);

                // Sending Student Id, Name, Number and Class to next UpdateActivity.
                intent.putExtra("idR", String.valueOf(idR));
                intent.putExtra("NomeLocale", Nomeristorante);
                intent.putExtra("PrezzoMedio", Prezzomedio);
                intent.putExtra("Nazione", Nazione);
                intent.putExtra("Regione", Regione);
                intent.putExtra("Citta", Citta);
                intent.putExtra("Provincia", Provincia);
                intent.putExtra("Cap", Cap);
                intent.putExtra("Indirizzo", Indirizzo);
                intent.putExtra("Email", Email);
                intent.putExtra("Telefono", Numero);


                startActivity(intent);

                // Finishing current activity after opening next activity.
                finish();
            }
        });

    }

    //Method to show current record Current Selected Record
    public void HttpWebCall(final String PreviousListViewClickedItem){

        class HttpWebCallFunction extends AsyncTask<String,Void,String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                pDialog = ProgressDialog.show(Modifica_info_base.this,"Loading Data",null,true,true);
            }

            @Override
            protected void onPostExecute(String httpResponseMsg) {

                super.onPostExecute(httpResponseMsg);

                pDialog.dismiss();

                //Storing Complete JSon Object into String Variable.
                FinalJSonObject = httpResponseMsg ;

                //Parsing the Stored JSOn String to GetHttpResponse Method.
                new GetHttpResponse(Modifica_info_base.this).execute();

            }

            @Override
            protected String doInBackground(String... params) {

                ResultHash.put("idR",params[0]);

                ParseResult = httpParse.postRequest(ResultHash, HttpURL);

                return ParseResult;
            }
        }

        HttpWebCallFunction httpWebCallFunction = new HttpWebCallFunction();

        httpWebCallFunction.execute(PreviousListViewClickedItem);
    }


    // Parsing Complete JSON Object.
    private class GetHttpResponse extends AsyncTask<Void, Void, Void>
    {
        public Context context;

        public GetHttpResponse(Context context)
        {
            this.context = context;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0)
        {
            try
            {
                if(FinalJSonObject != null)
                {
                    JSONArray jsonArray = null;

                    try {
                        jsonArray = new JSONArray(FinalJSonObject);

                        JSONObject jsonObject;

                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            jsonObject = jsonArray.getJSONObject(i);

                            // Storing Student Name, Phone Number, Class into Variables.
                            Nomeristorante = jsonObject.getString("NomeLocale").toString() ;
                            Prezzomedio = jsonObject.getString("PrezzoMedio").toString() ;
                            Nazione = jsonObject.getString("Nazione").toString() ;
                            Regione = jsonObject.getString("Regione").toString() ;
                            Citta = jsonObject.getString("Citta").toString() ;
                            Provincia = jsonObject.getString("Provincia").toString() ;
                            Cap = jsonObject.getString("Cap").toString() ;
                            Indirizzo = jsonObject.getString("Indirizzo").toString() ;
                            Email = jsonObject.getString("Email").toString() ;
                            Numero = jsonObject.getString("Telefono").toString() ;

                        }
                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {

            tNomeRistorante.setText(Nomeristorante);
            tPrezzoMedio.setText(Prezzomedio);
            tNazione.setText(Nazione);
            tRegione.setText(Regione);
            tCitta.setText(Citta);
            tCap.setText(Cap);
            tProvincia.setText(Provincia);
            tIndirizzo.setText(Indirizzo);
            tEmail.setText(Email);
            tNumero.setText(Numero);


        }
    }


}

最佳答案

使用OKhttp3图书馆。它很容易处理网络服务调用。并参阅本教程,了解如何从 json 编码文本获取数据 Json parser

关于php - 在 Android 应用程序中获取并显示单个用户的 MySQL 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55322720/

相关文章:

java - 无法连接 : MySQL Connector/J gives me a weird exception

MySQL 两列求和结果错误

php - 将帖子上传到 Wordpress 数据库时出错?

php - 在包中使用 Laravel 助手

php - 避免两次打开同一页面

android - xmlns :android ="http://schemas.android.com/apk/res/android" gets added to all my layout objects causing errors

php - 如何将 404 错误重定向到 404.php 页面?

java - Android 向服务器发出请求

java - Android Studio 检测到导入,但给出 "Symbol cannot be resolved"

php - 在 LEFT JOIN 子查询中显示 COUNT?