java - httpget 使用相同的应用程序和 PHP 文件从本地和互联网网络服务器获得不同的响应

标签 java android webserver

我正在尝试让 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 源代码。

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

最佳答案

您的本地 Web 服务器似乎没有安装或正确配置 php。请在此处查看帮助。
PHP: Installation and Configuration - Manual

关于java - httpget 使用相同的应用程序和 PHP 文件从本地和互联网网络服务器获得不同的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13208362/

相关文章:

java - 如何正确停止线程?

Android:在服务和 Activity 之间传递数据

mysql - 多表 - osticket

php - 使用 Phonegap 从 Android 上传照片

java - Oracle列名问题

java - 如何在android studio中隐藏应用程序名称?

java - JPA OneToMany 双向合并如何实现?

java - 如何检查Web服务器中的静态文件是否更新?

java - 如何以编程方式消除 SearchView 中 SearchView 提示图标占用的空间

java - JProgressBar 中的颜色