1

我正在尝试让 Android 设备使用 GET 方法发送一些 HTTP 请求。

这是我的代码:

package com.kde.httprequest;

import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class main2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText edit1 = (EditText) findViewById (R.id.editText1);
        Button btn1 = (Button) findViewById (R.id.button1);

        btn1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                grabURL(edit1.getText().toString());
            }
        });
    }

    public void grabURL(String url) {
        new GrabURL().execute(url);
    }

    private class GrabURL extends AsyncTask<String, Void, Void> {
        private final HttpClient Client = new DefaultHttpClient();
        private String Content;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(main2.this);
        final TextView text1 = (TextView) findViewById (R.id.textView1);

        protected void onPreExecute() {
            Dialog.setMessage("Downloading source..");
            Dialog.show();
        }

        protected Void doInBackground(String... urls) {
            try {
                HttpGet httpget = new HttpGet(urls[0]);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                Content = Client.execute(httpget, responseHandler);
            } catch (ClientProtocolException e) {
                Error = e.getMessage();
                cancel(true);
            } catch (IOException e) {
                Error = e.getMessage();
                cancel(true);
            }
            return null;
        }

        protected void onPostExecute(Void unused) {
            Dialog.dismiss();
            if (Error != null) {
                Toast.makeText(main2.this, Error, Toast.LENGTH_LONG).show();
                text1.setText(Error);
            } else {
                Toast.makeText(main2.this, "Source: " + Content, Toast.LENGTH_LONG).show();
                text1.setText(Content);
            }
        }
    }
}

我的简单 PHP 测试:

<?php

$a = $_GET['user'];
$b = $_GET['pass'];
if ($a=="usr" && $b=="pass") {
    echo "success";
} else {
    echo "fail";
}

?>

发送到此 URL 时,我的代码运行顺利:

digitalzone-btm.com/test2.php?user=user&pass=pass

我的 PHP 的响应是一个字符串,说“成功”或“失败”,这就是我所期望的。

但是我的本地网络服务器使用相同的 Android 应用程序和 PHP 文件得到了不同的响应。

前网址:

http://192.168.1.8/test2.php?user=user&pass=pass

响应正是我的 PHP 源代码。

如何从本地网络服务器获得“成功”或“失败”响应?

4

1 回答 1

0

您的本地 Web 服务器似乎没有正确安装或配置 php。检查这里寻求帮助。
PHP:安装和配置 - 手册

于 2012-11-03T11:41:03.973 回答