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() {
super
.onCreate();
System.out.println(
"TestService oncreate()"
);
}
@SuppressLint
(
"NewApi"
)
@Override
public
int
onStartCommand(Intent intent,
int
flags,
int
startId) {
getDate(
"http://www.lewei50.com/api/V1/Gateway/GetSensors/01"
);
return
super
.onStartCommand(intent, flags, startId);
}
@Override
public
void
onDestroy() {
super
.onDestroy();
System.out.println(
"TestService desory()"
);
}
@Override
public
IBinder onBind(Intent intent) {
return
myBind;
}
@Override
public
void
unbindService(ServiceConnection conn) {
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() {
}
@Override
public
void
onSuccess(
int
statusCode, Header[] headers,JSONObject response) {
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) {
super
.onFailure(arg0, headers, arg2, arg3);
Toast.makeText(GetDateService.
this
, arg3.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRetry() {
super
.onRetry();
}
});
System.out.println(url);
}
}