我尝试从JSON
数据库中提取MySQL
数据,但它给了我一个错误。我使用本地计算机并将端口配置为8080.所以我没有看到错误的位置。
<?php
$username = "root";
$password = "";
$host = "localhost";
$db_name = "user_info";
$conn = mysqli_connect($host,$username,$password,$db_name);
$sql = "select * from users where name like 'Rashmi';";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0)
{
$row = mysqli_fetch_assoc($result);
echo json_encode(array("Name"=>$row["name"],"Email"=>$row["email"],"Mobile"=>$row["mobile"]));
}
?>
这是android代码,它从数据库中获取数据为JSON。
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, json_url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
name.setText(response.getString("Name"));
email.setText(response.getString("Email"));
mobile.setText(response.getString("Mobile"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
MySingleton.getmInstance(MainActivity.this).addToRequestque(jsonObjectRequest);
}
答案 0 :(得分:0)
当您可以使用AsyncTask从MySQL数据库中检索所有数据时,我会给您解决方案。 试试这段代码: PHP脚本:首先将数据库中的所有元素添加到数组中:
<?php
$con = mysqli_connect("localhost", "db_username", "password", "database_name");
$query = mysqli_query($con, "SELECT username, email FROM users");
if($query)
{
while($row = mysqli_fetch_array($query))
{
$arrRow["username"] = $row["username"];
$arrRow["email"] = $row["email"];
$users[] = $arrRow;
}
print(json_encode($users));
}
mysqli_close($con);
&GT?; 您应该在JSON数组中获得所有结果。 接下来创建AsyncTask:
//Get Http Connection with async
private class RetrieveTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
String strUrl = "your url with php file";
URL url = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
iStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override
protected void onPostExecute(String result) {
ArrayList<HashMap<String, String>> location = null;
try {
JSONArray data = new JSONArray(result);
location = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("username", c.getString("username"));
map.put("email", c.getString("email")
location.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
if (location != null) {
for (int i = 0; i < location.size(); i++) {
username = location.get(i).get("username").toString();
email = location.get(i).get("email").toString();
//Here you can add these values to ArrayList and display it later
}
}
}
}
不要忘记在onCreate方法中或在您想要开始下载数据的任何地方初始化它。