我是使用Android应用程序的新手。我想从php文件获取数据到android,我不知道该怎么做。
我将使用一个简单的php代码示例,我想进入android:
$array = array(0 => 'zero', 1 => 'one', 2 => 'two');
print $array;
此脚本位于:http://johnyho.net/index.php
请你给我一个如何让这个字段进入android的建议。 非常感谢你。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
AndroidManifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.android.websevice.client.samples"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidClientService"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:2)
答案 1 :(得分:1)
使用HttpClient
HttpClient hc = new DefaultHttpClient();
HttpClient hc = new DefaultHttpClient();
HttpGet post = new HttpGet(" http://johnyho.net/index.php");
HttpResponse rp = hc.execute(post);
if (rp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
// Server is unavailable
}
str = EntityUtils.toString(rp.getEntity()); //here is your response
请导入必要的名称空间,例如
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;