简体   繁体   English

Android小部件HTTP请求

[英]Android widget HTTP request

I recently started a weather widget where i can read data from my weather station. 我最近启动了一个天气小部件,可以从气象站读取数据。

I want the plugin to get the newest data from the weather station when onUpdate is called. 我希望插件在调用onUpdate时从气象站获取最新数据。 I know you need a Http request, but i know very little about it so if anyone can show me how to do it, i would really appreciate. 我知道您需要一个Http请求,但是对此我知之甚少,因此,如果有人可以向我展示如何进行操作,我将非常感激。

I already have a php script which outputs the data so I just need to get in on the widget and insert in in the TextView. 我已经有一个输出数据的php脚本,所以我只需要进入小部件并插入TextView中即可。

I already tried many scripts on the web, but i didn't know how to call the class when the plugin updates and so on ... 我已经在网络上尝试了许多脚本,但是当插件更新等等时,我不知道如何调用该类……

I really appreciate any help! 我非常感谢您的帮助!

You can send asynchronous http request with : 您可以使用发送异步http请求:

private void getHttpRequest(){
    AsyncHttpClient asyncClient = new AsyncHttpClient();

    asyncClient.get(YOUR_URL_STR , new AsyncHttpResponseHandler(){
        @Override
        public void onStart(){ 
        }

        @Override 
        public void onSuccess(String response){
             // here you will call an other method that sets the values
        }
        @Override
        public void onFailure(Throwable e,String response){

        }

        @Override
        public void onFinish(){

    });
}

You can achieve that by downloading the loopj library 您可以通过下载loopj库来实现

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

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