码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-03-23 17:28:40      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:android在service中使用asynchttpclient加载网络资源

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” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1909711

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

标签:android在service中使用asynchttpclient加载网络资源

原文地址:http://matengbing.blog.51cto.com/11395502/1909711

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!