发送GET请求到PHP服务器

发送GET请求到PHP服务器

本文介绍了的Andr​​oid + PHP:发送GET请求到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送从Android应用到Web服务器的GET请求,这样服务器将存储在数据库中的信息。

I'm trying to send a GET request from an Android app to a web server, so the server will store the information in a database.

下面是在 Android应用程序

public class Final extends Activity {


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_final);


        Button btnSend = (Button)findViewById(R.id.BtnSend);



        btnSend.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0){

                TestReceiver();


            }
        });



    void TestReceiver()
    {

            //ALERT MESSAGE
            Toast.makeText(getBaseContext(),"Please wait, connecting to server.",Toast.LENGTH_LONG).show();

            HttpClient Client = new DefaultHttpClient();

            try
            {

                Client.getConnectionManager().getSchemeRegistry().register(getMockedScheme());

            } catch (Exception e) {
                System.out.println("Error con no se que");
            }


            String Value="Kevin";

            String URL = "http://echogame24.comli.com/Receiver.php?action="+Value;


            try
            {
                          String SetServerString = "";

                        // Create Request to server and get response

                         HttpGet httpget = new HttpGet(URL);
                         ResponseHandler<String> responseHandler = new BasicResponseHandler();
                         SetServerString = Client.execute(httpget, responseHandler);

             }
           catch(Exception ex)
           {
                    System.out.println("Fail!");
           }

    }



   //** I added all this following the advise in the link below


    public Scheme getMockedScheme() throws Exception {
        MySSLSocketFactory mySSLSocketFactory = new MySSLSocketFactory();
        return new Scheme("https", (SocketFactory) mySSLSocketFactory, 443);
    }

    class MySSLSocketFactory extends SSLSocketFactory {
        javax.net.ssl.SSLSocketFactory socketFactory = null;

        public MySSLSocketFactory(KeyStore truststore) throws Exception {
            super(truststore);
            socketFactory = getSSLSocketFactory();
        }

        public MySSLSocketFactory() throws Exception {
            this(null);
        }

        @Override
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
                UnknownHostException {
            return socketFactory.createSocket(socket, host, port, autoClose);
        }

        @Override
        public Socket createSocket() throws IOException {
            return socketFactory.createSocket();
        }

        javax.net.ssl.SSLSocketFactory getSSLSocketFactory() throws Exception {
            SSLContext sslContext = SSLContext.getInstance("TLS");

            TrustManager tm = new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            sslContext.init(null, new TrustManager[] { tm }, null);
            return sslContext.getSocketFactory();
        }
    }


}

在这个环节中接受的答案:

In the accepted answer of this link:

他们建议使用最后一个方法,使得应用程序可以发送请求的任何服务器。

They recommend to use the last methods so the application can send request to any server.

我还包括在 PHP服务器将接受请求的code:

I also include the code of the PHP server that will accept the request:

 <?php



$name="A_".$_GET["action"] ;




    require_once('mysql_conexion.php');

    //Here we set the variables

    $country = "Prueba";
    $score = 200;

    //Here we create the sql sentence we will need to insert information in the Data base
    $q="INSERT INTO PLAYERS(NAME, COUNTRY, SCORE) VALUES ('$name', '$country', '$score')";
    $r=@mysqli_query($dbc, $q);


    //Now we check if we succed or failed
    if($r){

    //This means we did it right

    echo 'There is a new player<br>';


    }else{

    //This means we failed

    echo '<h1>Error </h1>
    <p class="error">There was an error</p>';
    echo '<p>'.mysqli_error($dbc).'<br /><br />Query:'.$q.'</p>';

    }

    //We close now the data base connection

    mysqli_close($dbc);


?>

我的问题

包括了Android应用程序后,下面** 1,我有以下错误的方法:

After including in the Android Application the methods below **1 I have the following errors:

1 -

构造计划(字符串,Final.MySSLSocketFactory,INT)未定义

The constructor Scheme(String, Final.MySSLSocketFactory, int) is undefined

在该行:

return new Scheme("https", mySSLSocketFactory, 443);

2 -

在该行多个标记
     - 类型Final.MySSLSocketFactory必须实现继承的抽象方法SocketFactory.createSocket(InetAddress类,INT,
     InetAddress类,INT)
     - 类型Final.MySSLSocketFactory必须实现继承的抽象方法SocketFactory.createSocket(字符串,整数)
     - 类型Final.MySSLSocketFactory必须实现继承的抽象方法SSLSocketFactory.getSupportedCipherSuites()
     - 类型Final.MySSLSocketFactory必须实现继承的抽象方法SocketFactory.createSocket(InetAddress类,INT)
     - 类型Final.MySSLSocketFactory必须实现继承的抽象方法SocketFactory.createSocket(字符串,INT,InetAddress类,
     INT)
     - 类型Final.MySSLSocketFactory必须实现继承的抽象方法SSLSocketFactory.getDefaultCipherSuites()

Multiple markers at this line - The type Final.MySSLSocketFactory must implement the inherited abstract method SocketFactory.createSocket(InetAddress, int, InetAddress, int) - The type Final.MySSLSocketFactory must implement the inherited abstract method SocketFactory.createSocket(String, int) - The type Final.MySSLSocketFactory must implement the inherited abstract method SSLSocketFactory.getSupportedCipherSuites() - The type Final.MySSLSocketFactory must implement the inherited abstract method SocketFactory.createSocket(InetAddress, int) - The type Final.MySSLSocketFactory must implement the inherited abstract method SocketFactory.createSocket(String, int, InetAddress, int) - The type Final.MySSLSocketFactory must implement the inherited abstract method SSLSocketFactory.getDefaultCipherSuites()

在该行:

class MySSLSocketFactory extends SSLSocketFactory {

3 -

构造的SSLSocketFactory(密钥库)未定义

The constructor SSLSocketFactory(KeyStore) is undefined

在该行:

super(truststore);

谁能告诉我,我究竟做错了什么?我想我只是复制了code的那部分完全一样出现在这个问题的答案accpeted:

Could anyone tell me what am I doing wrong? I think I just copied that part of the code exactly like appear in the accpeted answer for the question:

推荐答案

您code可能是首先简化了,我建议你使用这个机器人库:
,让您尽样的HTTP请求。​​

Your code could be first of all simplified, i suggest you to use this android library:http://loopj.com/android-async-http/ which allows you to make every kind of HTTP requests.

在你下载了它,你应该很明显它导入到你的项目。

After you downloaded it you should obviously import it in your project.

code。与实施的Andr​​oid异步HTTP客户端

Code with Android Asynchronous Http Client implemented

public class Final extends Activity {

AsyncHttpClient client = new AsyncHttpClient();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_final);


    Button btnSend = (Button)findViewById(R.id.BtnSend);



    btnSend.setOnClickListener(new OnClickListener(){

    public void onClick(View arg0){

            TestReceiver();


        }
    });



void TestReceiver()
{

        //ALERT MESSAGE
        Toast.makeText(getBaseContext(),"Please wait, connecting to server.",Toast.LENGTH_LONG).show();
        String Value="Kevin";

        String url = "http://echogame24.comli.com/Receiver.php?action="+Value;


        client.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                Toast.makeText(getBaseContext(),"Request made successfully.",Toast.LENGTH_LONG).show();
            }
        });

    }
}


}

这篇关于的Andr​​oid + PHP:发送GET请求到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:21