通过Android应用程序运行php脚本

时间:2016-01-09 07:38:23

标签: php android androidhttpclient

我有一个存储在Xampp服务器中的php脚本,并希望我的应用程序执行它来执行任务。在eclipse中,服务器端的android.txt没有任何问题。 以下是我的Android应用程序代码

String url = "http://my.site.php?data=hello";
    HttpClient client = new DefaultHttpClient();
    try {
      client.execute(new HttpGet(url));
    } catch(IOException e) {
      //do something here
    }

String url = "http://my.site.php?data=hello"; HttpClient client = new DefaultHttpClient(); try { client.execute(new HttpGet(url)); } catch(IOException e) { //do something here }

以下是我在服务器端的PHP代码。

 $name=$_GET['data'];
    $file=fopen("./android.txt","w");
    fwrite($file, $name);
    fclose($file);
虽然我从mozila浏览器运行php,但它工作正常。但是这段代码不能用于android。

1 个答案:

答案 0 :(得分:3)

尝试这样的事情,你不是一个开放的连接,也没有读者阅读回复。现在,它首选在AsyncTask上运行外部连接,然后将响应返回给主线程。将响应设置为EditText或警告对话框,以便您可以看到服务器可能给您的任何错误。

Activity activity;
@Mock
FileDownloaderManager fileDownloaderManager;

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Before
    public void beforeTest() {

        // You intialise activity here
        activity = Robolectric.setupActivity(Activity.class);
    }

    @Test
    public void init_FileDownloaded() {

        // and use it here, the initialisation is out of scope for this
        ProgressTextView progressTextView = new ProgressTextView(activity);
    }
相关问题