使用php将数据从mysql检索到android
我创建了一个页面,显示Mysql数据库中存在的数据。我使用PHP进行连接。PHP文件包含select查询,用于显示数据库中的数据。PHP文件由android代码调用。完成JSON解析后,数据应显示在应用程序上。但问题是应用程序无法检索数据 在这里,java代码中没有一个错误。此外,我还执行了php文件localhost,它工作得非常好,在输出中,我得到了JSON数据 唯一的问题是JSON没有在应用程序上检索。请帮帮我。我被困在这里了。为了这个我试了一整天,但什么也没找到。我需要帮助,伙计们!!!如果你发现什么,一定要告诉我 代码: getIssue.php使用php将数据从mysql检索到android,php,android,mysql,json,Php,Android,Mysql,Json,我创建了一个页面,显示Mysql数据库中存在的数据。我使用PHP进行连接。PHP文件包含select查询,用于显示数据库中的数据。PHP文件由android代码调用。完成JSON解析后,数据应显示在应用程序上。但问题是应用程序无法检索数据 在这里,java代码中没有一个错误。此外,我还执行了php文件localhost,它工作得非常好,在输出中,我得到了JSON数据 唯一的问题是JSON没有在应用程序上检索。请帮帮我。我被困在这里了。为了这个我试了一整天,但什么也没找到。我需要帮助,伙计们!!!
<?php
//Importing Database Script
require_once('dbConfig.php');
//Creating sql query
$sql = "SELECT * FROM user_issue";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"id"=>$row['id'],
"store_name"=>$row['store_name'],
"issue"=>$row['issue'],
"describe"=>$row['describ']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
RequestHandler.java
package com.example.mi.mikpiadmin;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class RequestHandler {
//Method to send httpPostRequest
//This method is taking two arguments
//First argument is the URL of the script to which we will send the request
//Other is an HashMap with name value pairs containing the data to be send with the request
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
//Creating a URL
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Configuring connection properties
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null){
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public String sendGetRequest(String requestURL){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"\n");
}
}catch(Exception e){
}
return sb.toString();
}
public String sendGetRequestParam(String requestURL, String id){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL+id);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"\n");
}
}catch(Exception e){
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
RequestHandler是否返回了一些内容?我建议在PostExecute方法上设置一个断点,以查看从后端得到的响应。 另外,我个人更喜欢使用OkHTTP,对于一个简单的get请求,在您的doInBackground上会是这样的:
客户端应用程序和远程数据库之间通信的最佳方式是使用RESTfulAPI和著名的HTTP请求GET、PUT、POST和DELETE。当您有一个RESTAPI时,您可以使用同一个数据库创建多个客户端应用程序,如Android、IOS或JAVASCRIPT。它将由一个API_密钥保护,因此只有被授权进行查询或修改的请求才会被接受。 创建RESTAPI有很多方法,因为您是PHP开发人员,我建议您使用它,因为它轻量级且易于使用。
使用框架的另一个好处是安全性和sql注入问题,当初学者从头开始编写PHP代码时,会遇到很多问题 你有没有检查你的php代码是否有结果?你需要一步一步走。你确定这不仅仅是打字错误吗?getIssue.php!==见本期。php@jeroen字体对不起,这是打字mistake@PoorviGandhi:是的,我疯了。我检查了所有可能的东西。php文件工作正常。我正在将数据输入本地主机,请查看更新的代码。我还添加了请求处理程序的代码。
<?php
//Importing Database Script
require_once('dbConfig.php');
//Creating sql query
$sql = "SELECT * FROM user_issue";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"id"=>$row['id'],
"store_name"=>$row['store_name'],
"issue"=>$row['issue'],
"describe"=>$row['describ']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
RequestHandler.java
package com.example.mi.mikpiadmin;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class RequestHandler {
//Method to send httpPostRequest
//This method is taking two arguments
//First argument is the URL of the script to which we will send the request
//Other is an HashMap with name value pairs containing the data to be send with the request
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
//Creating a URL
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Configuring connection properties
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null){
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public String sendGetRequest(String requestURL){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"\n");
}
}catch(Exception e){
}
return sb.toString();
}
public String sendGetRequestParam(String requestURL, String id){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL+id);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"\n");
}
}catch(Exception e){
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(Config.URL_GET_ISSUE)
.build();
Response response = client.newCall(request).execute();
return response.body().string();