我是 Android 初学者,我正在尝试将我的 Android 应用程序连接到一个返回 JSON 对象的简单 php。
每当我运行我的应用程序时,我都会收到错误不幸的是应用程序已在模拟器上停止,并且在ABD LOG中我收到PropertyFetcher:AdbCommandRejectedException正在获取设备的属性emulator-5554:设备离线
-
**MainActivity.java**
---------------------
package jsonphp.com.jsonphp;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends ActionBarActivity {
private TextView responseTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.responseTextView = (TextView) this.findViewById(R.id.responseTextView);
new GetAllCustomerTask().execute(new ApiConnector());
}
public void setTextToTextView(JSONArray jsonArray)
{
String s = "";
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
s = s +
"Name : "+jsonObject.getString("name")+"\n"
+ "Age : "+jsonObject.getString("age")+"\n"
+ "Address : "+jsonObject.getString("address")+"\n\n";
}
catch (JSONException e)
{
e.printStackTrace();
}
}
this.responseTextView.setText(s);
}
private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
//this execute a bg thread
return params[0].getAllCustomers();
}
protected void onPostExecute(JSONArray jsonArray)
{
setTextToTextView(jsonArray);
}
}
}
-
**ApiConnector.java**
---------------------
package jsonphp.com.jsonphp;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
/**
* Created by SWAPNIL on 20-06-2015.
*/
public class ApiConnector {
public JSONArray getAllCustomers()
{
//URL for getting all customers
String URL = "http://127.0.0.1/Android/test.php";
// Get HttpResponse object from URL
// Get HttpEntity from HTTP Response object
HttpEntity httpEntity = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
}
catch (ClientProtocolException ex)
{
ex.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
//Converting HttpEntity into JSONArray
JSONArray jsonArray = null;
if(httpEntity != null)
{
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ",entityResponse);
jsonArray = new JSONArray(entityResponse);
}
catch (JSONException ee)
{
ee.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return jsonArray;
}
}
这是我的简单布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/responseTextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="60dp"
android:textAlignment="center"/>
</RelativeLayout>
还有我的 test.php 文件。
<?php
$con = mysqli_connect("localhost","root","root","My_Database");
if(!$con)
{
die("Could not connect : ".mysql_error());
}
/*mysqli_select_db("My_Database",$con);*/
$result = mysqli_query($con,"SELECT * From Customer");
while($row=mysqli_fetch_assoc($result))
{
$output[] = $row;
}
print(json_encode($output));
mysqli_close($con);
?>
我还考虑过一件事 HttpEntity, 默认HttpClient, HttpGet, Http响应, 客户端协议(protocol)异常, 实体实用程序 以上均已弃用
最佳答案
首先你应该检查返回的结果是json数组格式还是json对象格式。 您正在使用的 android 代码用于解析 json 数组。从 php 代码中我得到的是,你返回的 json 不是一个 json 数组。 将其转换为 json 数组,然后在 android 中使用您当前使用的相同代码来解析它。
第二个错误是您使用的是本地主机。 您的设备或模拟器无法与该服务器建立连接。 使用 ip 10.0.0.1 而不是 127.0.0.01 或使其在线
关于php - 安卓 PHP MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955864/