【问题标题】:Android + PHP: Sending a GET request to PHP serverAndroid + PHP:向 PHP 服务器发送 GET 请求
【发布时间】:2014-05-27 22:33:53
【问题描述】:

我正在尝试从 Android 应用向网络服务器发送 GET 请求,因此服务器会将信息存储在数据库中。

这是 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();
        }
    }


}

在此链接的接受答案中:

Android - HTTP GET Request

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

我还包括将接受请求的 PHP 服务器 的代码:

 <?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 我有以下错误:

1-

构造函数 Scheme(String, Final.MySSLSocketFactory, int) 未定义

排队:

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

2-

此行有多个标记 - Final.MySSLSocketFactory 类型必须实现继承的抽象方法 SocketFactory.createSocket(InetAddress, int, 网络地址,整数) - Final.MySSLSocketFactory 类型必须实现继承的抽象方法 SocketFactory.createSocket(String, int) - Final.MySSLSocketFactory 类型必须实现继承的抽象方法 SSLSocketFactory.getSupportedCipherSuites() - Final.MySSLSocketFactory 类型必须实现继承的抽象方法 SocketFactory.createSocket(InetAddress, int) - Final.MySSLSocketFactory 类型必须实现继承的抽象方法 SocketFactory.createSocket(String, int, InetAddress, 诠释) - Final.MySSLSocketFactory 类型必须实现继承的抽象方法 SSLSocketFactory.getDefaultCipherSuites()

排队:

class MySSLSocketFactory extends SSLSocketFactory {

3-

构造函数 SSLSocketFactory(KeyStore) 未定义

行内:

super(truststore);

谁能告诉我我做错了什么?我想我只是复制了代码的那部分,就像问题的可接受答案中出现的那样:

Android - HTTP GET Request

【问题讨论】:

  • 尝试使用 org.apache.http.conn.ssl.SSLSocketFactory 而不是 javax.net.ssl.SSLSocketFactory。

标签: php android get


【解决方案1】:

您的代码首先可以简化,我建议您使用这个 android 库: http://loopj.com/android-async-http/ 允许您发出各种 HTTP 请求。

下载后,您显然应该将其导入您的项目中。

实现了 Android 异步 Http 客户端的代码

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();
            }
        });

    }
}


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    相关资源
    最近更新 更多