如何阅读PHP code的响应到Android的Java code [英] How to read response of php code to android java code
问题描述
我有返回数据库中的所有内容作为一个JSON数组WAMP的服务器上运行一个PHP code。我想我的Android应用程序连接到这个PHP文件。我如何获得这个PHP文件到Android的Java code的响应。
我的PHP code:
< PHP$ CON = mysqli_connect(localhost的根,2015,testdatabase)或死亡(错误mysqli_error($链接)。);
如果(!$ CON)
{
死亡(无法连接:'mysqli_error());
}
$结果= mysqli_query($ CON,SELECT * FROM客户);
而($行= mysqli_fetch_assoc($结果))
{
$输出[] = $行;
}
打印(json_en code($输出));
mysqli_close($ CON);
?>
另外这里是我目前在做我的Android code(但大多数我在做什么被视为德precated):
公共JSONArray GetAllCustomers()
{
// URL获取所有客户
字符串URL =http://127.0.0.1/Customers/getAllCustomers.php; //获取的Htt presponse从URL对象。
//从HTTP响应对象HttpEntity HttpEntity httpEntity = NULL; 尝试
{ DefaultHttpClient的HttpClient =新DefaultHttpClient(); //默认的HttpClient
HTTPGET HTTPGET =新HTTPGET(URL); HTT presponse HTT presponse = httpClient.execute(HTTPGET); httpEntity = HTT presponse.getEntity(); }赶上(ClientProtocolException E){ //在HTTP协议信号的错误
e.printStackTrace(); //登录错误在这里 }赶上(IOException异常五){
e.printStackTrace();
}
//转换HttpEntity成JSON阵列
JSONArray jsonArray = NULL; 如果(httpEntity!= NULL){
尝试{
字符串entityResponse = EntityUtils.toString(httpEntity); Log.e(实体回应:entityResponse); jsonArray =新JSONArray(entityResponse); }赶上(JSONException E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
} 返回jsonArray; }
}
我也可以根据这个教程与Android连接PHP Web服务(JSON格式)。我认为它也会对你有所帮助。
I have a php code running on a wamp server which returns all the contents of database as a JSON array. I want to connect my android app to this php file. How do i get the response of this php file to my android java code.
MY PHP CODE:
<?php
$con = mysqli_connect("localhost","root","2015","testdatabase") or die("Error " . mysqli_error($link));
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
$result = mysqli_query($con,"SELECT * FROM Customer");
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysqli_close($con);
?>
Also here is what I'm currently doing in my android code (But most of what i am doing is deemed deprecated):
public JSONArray GetAllCustomers()
{
// URL for getting all customers
String url = "http://127.0.0.1/Customers/getAllCustomers.php";
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
//Log Errors Here
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
}
I also connect php web services(Json formatted) with android according to this tutorial. I think it will also helpful to you also.
How to connect Android with PHP, MySQL
这篇关于如何阅读PHP code的响应到Android的Java code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!