android在service中使用AsyncHttpClient加载网络资源

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package  com.example.service;
import  org.apache.http.Header;
import  org.json.JSONObject;
 
 
 
 
 
 
import  com.loopj.android.http.AsyncHttpClient;
 
import  com.loopj.android.http.JsonHttpResponseHandler;
import  com.loopj.android.http.RequestParams;
 
import  android.annotation.SuppressLint;
import  android.app.Service;
import  android.content.Intent;
import  android.content.ServiceConnection;
import  android.os.Binder;
import  android.os.IBinder;
 
import  android.widget.Toast;
 
 
public  class  GetDateService  extends  Service{
     /*
      * 请求队列*/
 
     private  MyBind myBind= new  MyBind();
     
     @Override
     public  void  onCreate() {
         // TODO Auto-generated method stub
         super .onCreate();
         
         
         System.out.println( "TestService oncreate()" );
     }
     @SuppressLint ( "NewApi" )
     @Override
     public  int  onStartCommand(Intent intent,  int  flags,  int  startId) {
         // TODO Auto-generated method stub
         getDate( "http://www.lewei50.com/api/V1/Gateway/GetSensors/01" );
         return  super .onStartCommand(intent, flags, startId);
     }
     @Override
     public  void  onDestroy() {
         // TODO Auto-generated method stub
         super .onDestroy();
         
         System.out.println( "TestService desory()" );
     }
     @Override
     public  IBinder onBind(Intent intent) {
         // TODO Auto-generated method stub
         return  myBind;
     }
     @Override
     public  void  unbindService(ServiceConnection conn) {
         // TODO Auto-generated method stub
         System.out.println( "Test UnbindService()" );
         super .unbindService(conn);
     }
     
     public  class  MyBind  extends  Binder{
         public  void  test(){
             System.out.println( "test" );
         }
         public  GetDateService getDateService(){
             return  GetDateService. this ;
         }
     }
     public  void  getDate(String url){
         AsyncHttpClient client =  new  AsyncHttpClient();
          RequestParams params =  new  RequestParams();
             // 设置请求的参数名和参数值
          params.put( "userKey" "cc809cd707xxxxxa917xxxxxxc674d0" );
         client.get(url,params,  new  JsonHttpResponseHandler() {
             @Override
             public  void  onStart() {
                 // called before request is started
                 
             }
 
             @Override
             public  void  onSuccess( int  statusCode, Header[] headers,JSONObject response) {
                 // TODO Auto-generated method stub
                 super .onSuccess(statusCode, headers, response);
                 Toast.makeText(GetDateService. this , response.toString(), Toast.LENGTH_SHORT).show();
             }
             @Override
             public  void  onFailure( int  arg0, Header[] headers,  byte [] arg2,
                     Throwable arg3) {
                 // TODO Auto-generated method stub
                 super .onFailure(arg0, headers, arg2, arg3);
                 Toast.makeText(GetDateService. this , arg3.toString(), Toast.LENGTH_SHORT).show();
                 
             }
             @Override
             public  void  onRetry() {
                 // TODO Auto-generated method stub
                 super .onRetry();
             }
         });
         
         System.out.println(url);
     
     
         
     }
     
     
     
}

public void getDate(String url){
        AsyncHttpClient client = new AsyncHttpClient();
         RequestParams params = new RequestParams();
            // 设置请求的参数名和参数值
         params.put("userKey", "cc809cd707cexxxxx9176xxxxxc674d0");
        client.get(url,params, new JsonHttpResponseHandler() {
            @Override
            public void onStart() {
                // called before request is started
                
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers,JSONObject response) {
                // TODO Auto-generated method stub
                super.onSuccess(statusCode, headers, response);
                Toast.makeText(GetDateService.this, response.toString(), Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFailure(int arg0, Header[] headers, byte[] arg2,
                    Throwable arg3) {
                // TODO Auto-generated method stub
                super.onFailure(arg0, headers, arg2, arg3);
                Toast.makeText(GetDateService.this, arg3.toString(), Toast.LENGTH_SHORT).show();
                
            }
            @Override
            public void onRetry() {
                // TODO Auto-generated method stub
                super.onRetry();
            }
        });
       
    }

参数格式是String类型,得到的数据格式是json格式



 本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1909711


相关文章
|
4天前
|
Android开发
Android网络访问超时
Android网络访问超时
13 2
|
4天前
|
Java Linux API
统计android设备的网络数据使用量
统计android设备的网络数据使用量
16 0
|
4天前
|
安全 网络安全 数据安全/隐私保护
【专栏】IT 知识百科:访问控制列表(ACL)是网络安全的关键机制,用于定义和管理网络资源的访问权限
【4月更文挑战第28天】访问控制列表(ACL)是网络安全的关键机制,用于定义和管理网络资源的访问权限。ACL工作原理包括定义规则、匹配规则和执行操作。标准ACL基于源IP过滤,扩展ACL则提供更多筛选条件。时间及用户基础的ACL提供更细化的控制。优点在于增强安全性和精细管理,但管理复杂性和性能影响也是挑战。未来,ACL将趋向智能化和自动化,与更多安全技术结合,以提升网络安全。**
|
2天前
|
设计模式 算法 Android开发
2024年Android网络编程总结篇,androidview绘制流程面试
2024年Android网络编程总结篇,androidview绘制流程面试
2024年Android网络编程总结篇,androidview绘制流程面试
|
3天前
|
存储 监控 Java
Android Service之设备存储空间监控 DeviceStorageMonitorService
Android Service之设备存储空间监控 DeviceStorageMonitorService
18 2
|
4天前
|
Android开发 数据库管理
Android如何在Activity和Service之间传递数据
Android如何在Activity和Service之间传递数据
11 3
|
4天前
|
Android开发
android检测网络连接是否存在(一)
android检测网络连接是否存在(一)
12 2
|
4天前
|
编解码 调度 Android开发
Android音频框架之一 详解audioPolicy流程及HAL驱动加载与配置
Android音频框架之一 详解audioPolicy流程及HAL驱动加载与配置
19 0
|
4天前
|
Android开发
Android中Glide加载Https图片失败的解决方案
Android中Glide加载Https图片失败的解决方案
18 1
|
4天前
|
Android开发
Android Service的两种使用方法
Android Service的两种使用方法
16 2

热门文章

最新文章