gpt4 book ai didi

php - 从php/mysql中获取日期和时间格式的数据,它应该显示在android studio的textview中

转载 作者:行者123 更新时间:2023-11-30 23:44:33 24 4
gpt4 key购买 nike

我需要从我的 sql 数据库中获取 scheduleDateStartTimeEndtimeHours,它们是以日期和时间格式显示,我需要在 Android Studio 的 TextView 中显示它们。但我无法在 textview 中显示它们。请任何人帮助我。

mysql database pic

my php code which will fetch the data`
<?php
error_reporting(0);
require "init.php";

$name = $_POST["name"];
$password = $_POST["password"];

//$name = "sdf";
//$password = "sdf";

$sql = "SELECT * FROM `user_info` WHERE `name`='".$name."' AND `password`='".$password."';";

$result = mysqli_query($con, $sql);

$response = array();

while($row = mysqli_fetch_array($result)){
$response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3],"ScheduleDate"=>$row[4],"StartTime"=>$row[5],"Endtime"=>$row[6],"Hours"=>$row[7]);
}

echo json_encode(array("user_data"=>$response));

?>

和我的 android 代码主要 Activity :

package com.example.myapplication;

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.EditText;

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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MainActivity extends AppCompatActivity {

EditText name, password;
String Name, Password,ScheduleDate,StartTime,Endtime,Hours;
Context ctx=this;
String NAME=null, PASSWORD=null, EMAIL=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.main_name);
password = (EditText) findViewById(R.id.main_password);
}

public void main_register(View v){
// startActivity(new Intent(this,Register.class));
}

public void main_login(View v){
Name = name.getText().toString();
Password = password.getText().toString();
BackGround b = new BackGround();
b.execute(Name, Password);
}

class BackGround extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... params) {
String name = params[0];
String password = params[1];
String data="";
int tmp;

try {
URL url = new URL("http://localhost/sample/loo/login.php");
String urlParams = "name="+name+"&password="+password;

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();

InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}

is.close();
httpURLConnection.disconnect();

return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}

@Override
protected void onPostExecute(String s) {
String err=null;
try {
JSONObject root = new JSONObject(s);
JSONObject user_data = root.getJSONObject("user_data");
NAME = user_data.getString("name");
PASSWORD = user_data.getString("password");
EMAIL = user_data.getString("email");

ScheduleDate = user_data.getString("ScheduleDate");
StartTime = user_data.getString("StartTime");
Endtime = user_data.getString("Endtime");
Hours = user_data.getString("Hours");
} catch (JSONException e) {
e.printStackTrace();
err = "Exception: "+e.getMessage();
}

Intent i = new Intent(ctx, Home.class);
i.putExtra("name", NAME);
i.putExtra("password", PASSWORD);
i.putExtra("email", EMAIL);

i.putExtra("ScheduleDate", ScheduleDate);
i.putExtra("StartTime", StartTime);
i.putExtra("Endtime", Endtime);
i.putExtra("Hours", Hours);

i.putExtra("err", err);
startActivity(i);

}
}
}`

这是我的显示页面 Activity :

    package com.example.myapplication;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class Home extends AppCompatActivity {

String name, password, email, Err,ScheduleDate,StartTime,Endtime,Hours;
TextView nameTV, emailTV, passwordTV, err,ScheduleDateTV,StartTimeTV,EndtimeTV,HoursTV;

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

nameTV = (TextView) findViewById(R.id.home_name);
emailTV = (TextView) findViewById(R.id.home_email);
passwordTV = (TextView) findViewById(R.id.home_password);

ScheduleDateTV = (TextView) findViewById(R.id.ScheduleDate);
StartTimeTV = (TextView) findViewById(R.id.StartTime);
EndtimeTV = (TextView) findViewById(R.id.Endtime);
HoursTV = (TextView) findViewById(R.id.Hours);
err = (TextView) findViewById(R.id.err);

name = getIntent().getStringExtra("name");
password = getIntent().getStringExtra("password");
email = getIntent().getStringExtra("email");
ScheduleDate = getIntent().getStringExtra("ScheduleDate");
StartTime = getIntent().getStringExtra("StartTime");
Endtime = getIntent().getStringExtra("Endtime");
Hours = getIntent().getStringExtra("Hours");
Err = getIntent().getStringExtra("err");

nameTV.setText("Welcome "+name);
passwordTV.setText("Your password is "+password);
emailTV.setText("Your email is "+email);
ScheduleDateTV.setText("your date "+ScheduleDate);
StartTimeTV.setText("starting time "+StartTime);
EndtimeTV.setText("ending time "+Endtime);
HoursTV.setText("total hours "+Hours);

err.setText(Err);
}
}

最佳答案

您的代码很好。用户名 selvaMySql

上没有 scheduleDate、StartTime、Endtime 数据

因此请尝试使用用户名 surya 登录,这可能会根据您的需要返回响应。

用用户 selvaMySql 上添加 scheduleDate ,StartTime,Endtime 数据

关于php - 从php/mysql中获取日期和时间格式的数据,它应该显示在android studio的textview中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47806572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com