2011-09-15 126 views
2

我是使用android应用程序的新手。我想从一个PHP文件到Android的数据,我不知道该怎么做。将数据从php文件传输到Android应用程序

我将使用的PHP代码一个简单的例子,我想进入安卓

$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

喜,最好是使用JSON,然后你可以通过分析执行PHP文件的结果得到的那场, – Houcine

+0

谢谢......它不工作......我不知道在xml文件中是否有什么不好的东西......现在也在这里。 –

+0

在我现在的php文件中:$ my_array = array(0 =>'zero',1 =>'one',2 =>'two'); $ json_string = json_encode($ my_array); print $ json_string; .................所以我得到输出[“zero”,“one”,“two”] –

回答

2

考虑JSON。 HERE是一个PHP JSON文档,而HERE是一个从Android到任何JSON url的连接示例,可能是您的URL。

+0

这是他的问题的更好的解决方案。 – Houcine

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; 
相关问题