在Android Studio中使用HttpGet加载PHP的问题

时间:2014-11-10 02:00:23

标签: php android

我对使用Android Studio进行开发相当陌生,所以我很可能会发现这个完全错误...

我的PHP脚本运行良好:

<?php
$con=mysqli_connect("xxx.178.143.xxx","roomapp","xxx","roomapp");
date_default_timezone_set('America/New_York');
$testschedulesql = "SELECT * from raRmSchedule WHERE '" . date('Y-m-d G:i:s') . "' > rsintime and '" . date('Y-m-d G:i:s') . "' < rsouttime";
$testscheduleqry = mysqli_query($con, $testschedulesql);
$testschedule = mysqli_num_rows($testscheduleqry);
$testscheduletext = mysqli_fetch_array($testscheduleqry);
if($testschedule > 0){
    echo'Busy';
}
else{
    echo'Free';
}
?>

我希望这能在我的Android应用中运行。到目前为止,我的java文件看起来像这样:

package com.sinteksolutions.roomapp2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;


public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            BufferedReader in = null;
                            try {
                                HttpClient client = new DefaultHttpClient();
                                HttpGet request = new HttpGet();
                                URI website = new URI("http://sinteksolutions.com/rmscheduler/timetest.php");
                                request.setURI(website);
                                HttpResponse response = client.execute(request);
                                in = new BufferedReader(new InputStreamReader(
                                        response.getEntity().getContent()
                                ));
                                String line = in.readLine();
                                TextView rmstatus = (TextView) findViewById(R.id.therm);
                                rmstatus.setText(line);
                            } catch (Exception e) {
                                //do something here
                            }

                        }
                    });
                } catch (InterruptedException e) {

                }
            }
        });

        thread.start();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的XML:

<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=".MyActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/therm"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

我有互联网权限设置。

有时这在模拟器中工作正常并显示文本。有时它什么也没显示。但该应用程序始终运行。

我做错了什么?或者有更好的方法吗?谢谢!

0 个答案:

没有答案