gpt4 book ai didi

php - 使用PHP从android连接mysql

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:44 27 4
gpt4 key购买 nike

可能是包含以下代码的 php 文件有问题:

    <?php
mysql_connect("localhost","root","");
mysql_select_db("deal");
$sql=mysql_query("select * from CITY");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

检索数据库中所有城市的简单代码。我把这个文件放在:C:\Program Files\EasyPHP-5.3.9\www\city.php

java代码很简单,连接并显示查询结果:

package com.udios.mysql;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MySQLActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void onTestClick(View view) {
/*Toast.makeText(MySQLActivity.this, "Test Pressed", Toast.LENGTH_SHORT)
.show();*/

this.runOnUiThread(new Runnable() {

@Override
public void run() {
String result = null;

JSONArray jArray;
InputStream is = null;
StringBuilder sb = null;
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://127.0.0.1/city.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");

String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();

int ct_id;
String ct_name;
Toast.makeText(getBaseContext(), "result is: "+result,
100).show();
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
ct_id = json_data.getInt("CITY_ID");
ct_name = json_data.getString("CITY_NAME");


Toast.makeText(getBaseContext(), Integer.toString(ct_id)+" : " + ct_name ,
Toast.LENGTH_LONG).show();

}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No City Found",
Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}

}
});

}
}

当我尝试在浏览器中直接访问时:

我得到:注意: undefined variable :C:\Program Files\EasyPHP-5.3.9\www\city.php 第7行输出空

我在 android 中调试,这一行抛出异常:(最后捕获)

HttpPost httppost = new HttpPost("http://127.0.0.1/city.php");

如果我将它更改为 10.0.2.2,它会在这一行抛出异常:

HttpResponse response = httpclient.execute(httppost);

我真的尝试了所有组合,有人可以帮助我吗?非常感谢乌迪

最佳答案

第一次向 $output 添加一行时,它是未定义的。

在您的 PHP 代码中的 while 循环之前添加它。

$output = array();

如果您在模拟器上运行,您还需要连接 10.0.2.2,或者在真实设备上连接您的本地 IP(确保该设备通过 WiFi 连接到您的网络)。

关于php - 使用PHP从android连接mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10724742/

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