【问题标题】:Remote button click (cgi script running)远程按钮单击(cgi 脚本运行)
【发布时间】:2013-06-23 19:10:43
【问题描述】:

我有一个具有切换功能的网络设备。当我单击设备自己的网页时,某些输出是更改状态。 我想创建一个可以在网络上管理此设备的 Android 应用程序。我会在我的应用程序上单击一个按钮,并且此事件在设备上运行,单击设备网页上的一个按钮。 这是设备网页上的代码:

<td width="130px"><input type="button"style="width:90px;"onclick="newAJAXCommand('leds.cgi?led=2');"value="Relé 1"/></td> 

我对 Android 应用有所了解,但不知道如何运行此查询(或创建点击事件)?

设备:http://91.137.148.13:8000/switch.htm

请帮忙。

问候, 久拉

【问题讨论】:

    标签: android html web


    【解决方案1】:

    当您点击网页上的按钮时,浏览器会向指定的 url (http://91.137.148.13:8000/leds.cgi?led=0) 发送请求。因此,当用户单击您的 Android 应用中的按钮时,您可以向此 url 发送请求。

    private class exampleHttpTask extends AsyncTask<Integer, Integer, String> {
    public String convertStreamToString(InputStream is, String charset) throws IOException {
        if (is != null) {
            Writer writer = new StringWriter();
            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is, charset));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
    
    protected String doInBackground(Integer... params) {
        String r = "";
        try {
            HttpClient hc = new DefaultHttpClient();
            HttpGet get = new HttpGet("http://91.137.148.13:8000/leds.cgi?led=0");
            HttpResponse hr = hc.execute(get);
    
            if(hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream is = hr.getEntity().getContent();
                r = convertStreamToString(is, "UTF-8");
            } else {
                r = "Error";
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    protected void onPostExecute(String result) {
        Log.d("WEB", result);
    }
    
    protected void onPreExecute() {
    }
    
    }
    

    (代码来自here) 并在触发 onClick 事件时,运行

    new exampleHttpTask().exec()

    【讨论】:

    • 非常感谢大卫。我要测试一下。问候,久拉
    • 大卫,我已经用网络浏览器测试了一个链接91.137.148.13:8000/leds.cgi?led=2,它工作正常。你解决了我的问题。非常感谢。 Rgards, Gyula
    猜你喜欢
    • 2017-02-27
    • 2017-04-08
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2014-07-08
    • 2017-06-02
    相关资源
    最近更新 更多