简体   繁体   English

Android-HTTP GET请求

[英]Android - HTTP GET request

I am trying to make an android app which can send a GET request to a server on a local network. 我正在尝试制作一个可以将GET请求发送到本地网络上的服务器的android应用。 I have found an example here , and used the GET example. 在这里找到了一个示例,并使用了GET示例。 I changed the getURL string to be the address I want to send to (192.168.1.177/?testGET). 我将getURL字符串更改为要发送到的地址(192.168.1.177/?testGET)。 I then attached the function to a buttons onClick event, but when I test the app on my phone, the app closes as soon as I press the button. 然后,我将该功能附加到按钮onClick事件上,但是当我在手机上测试该应用程序时,只要按下该按钮,该应用程序就会关闭。 See below for the code. 参见下面的代码。

public void sendGETrequest(){
    try {
        HttpClient client = new DefaultHttpClient();  
        String getURL = "192.168.1.177/?testGET" ;
        HttpGet get = new HttpGet(getURL);
        HttpResponse responseGet = client.execute(get);  
        HttpEntity resEntityGet = responseGet.getEntity();  
        if (resEntityGet != null) {  
                    //do something with the response
                    Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
                }
} catch (Exception e) {
    e.printStackTrace();
}
}

Why might this not be working? 为什么这可能不起作用? Also is there a way to dynamically add parameters to the end of the URL to be read by the server. 还有一种方法可以将参数动态添加到服务器要读取的URL的末尾。
See below for the output of the console window 参见以下控制台窗口的输出
[2014-01-19 15:44:25 - sensorTest] ------------------------------
[2014-01-19 15:44:25 - sensorTest] Android Launch!
[2014-01-19 15:44:25 - sensorTest] adb is running normally.
[2014-01-19 15:44:25 - sensorTest] Performing com.example.sensortest.MainActivity activity launch
[2014-01-19 15:44:51 - sensorTest] Uploading sensorTest.apk onto device 'P772N10_VIRGIN'
[2014-01-19 15:44:51 - sensorTest] Installing sensorTest.apk...
[2014-01-19 15:44:58 - sensorTest] Success!
[2014-01-19 15:44:58 - sensorTest] Starting activity com.example.sensortest.MainActivity on device P772N10_VIRGIN

When I open the app and press the button, it closes with the message "Unfortunately, sensor test has stopped." 当我打开应用程序并按下按钮时,它关闭并显示消息“不幸的是,传感器测试已停止。”

Without a logcat, it's hard to say what is causing the crash. 没有logcat,很难说是导致崩溃的原因。 But I'm quite certain that it's because you are doing networking on the main thread. 但是我可以肯定这是因为您正在主线程上进行网络连接。

See NetworkingOnMainThreadException 请参见NetworkingOnMainThreadException

It doesn't necessarily explain much, but basically, you can't do network (or long running) tasks on your main thread as it potentially hangs the UI. 它不一定说明太多,但是基本上,您不能在主线程上执行网络(或长时间运行)任务,因为它可能会挂起UI。 This is probably a better explanation though. 不过,这可能是一个更好的解释。

As for 'dynamically adding parameters', you could just change it to something like this: 至于“动态添加参数”,您可以将其更改为如下所示:

public void sendGETRequest(String paramaters){
    ...

    String getURL = "192.168.1.177/?testGET&" + paramaters;

    ...
}

and you can pass parameters with the method call like so: 您可以像这样通过方法调用传递参数:

sendGETRequest("username=admin&password=123Password321!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM