Created
November 6, 2012 07:47
-
-
Save Matsushige/4023293 to your computer and use it in GitHub Desktop.
データ送信スレッド
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SendDataService extends IntentService { | |
HttpURLConnection httpConnect = null; | |
public static String readData = ""; | |
public static String serial = ""; | |
public static String type = ""; | |
public static String id = ""; | |
public | |
public SendDataService() { | |
super("sendDataService"); | |
}// SendDataService | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
while (true) { | |
String connect; | |
readDatabase(getApplicationContext()); | |
if (readData != "") { | |
connect = httpConnect(id, type); | |
if (connect == "OK") { | |
writeDatabase(getApplicationContext(), serial); | |
}// if | |
} else { | |
Log.d("Service", "ALL_1"); | |
break; | |
}// else | |
try { | |
Thread.sleep(66000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
Log.e("Service", e.toString()); | |
}// catch | |
}// while | |
Log.d("Service", "END_SERVICE"); | |
}// onHandleIntent | |
public String httpConnect(String id, String type) { | |
String returnValue = ""; | |
try { | |
String baseUrl = "http://アドレス/test.cgi"; | |
String idParameter = "id=" + id; | |
String typeParameter = "type=" + type; | |
String outletIdParameter = "outletId=" + ControlTransmissionActivity.outletId; | |
URL url = new URL(baseUrl + "?" + idParameter + "&" + typeParameter + "&" + outletIdParameter); | |
URLConnection connect = url.openConnection(); | |
httpConnect = (HttpURLConnection) connect; | |
httpConnect.connect(); | |
int response = httpConnect.getResponseCode(); | |
/** OK(200)であるか判定 */ | |
if (response == HttpURLConnection.HTTP_OK) { | |
returnValue = "OK"; | |
Log.d("Service", "HTTP : OK (" + response + ")"); | |
} else { | |
returnValue = "ERROR"; | |
Log.d("Service", "HTTP : NOT_OK (" + response + ")"); | |
}// else | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
Log.e("Service", e.toString()); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
Log.e("Service", e.toString()); | |
} finally { | |
httpConnect.disconnect(); | |
} | |
return returnValue; | |
}// httpConnect | |
/** 送信のためのデータベース読み出し(send_checkが0の行を1つ取り出す) */ | |
public void readDatabase(Context context){ | |
readData = ""; | |
SQLiteDatabase db = (new sendDatabaseHelper(context)).getReadableDatabase(); | |
String selection = "send_check = ?"; | |
String[] selectionArg = {"0"}; | |
Cursor c = db.query("data", null, selection, selectionArg, null, null, null); | |
if (c.getCount() > 0) { | |
c.moveToFirst(); | |
serial = c.getString(c.getColumnIndex("serial")); | |
type = c.getString(c.getColumnIndex("type")); | |
id = c.getString(c.getColumnIndex("id")); | |
readData = serial + "," + type + "," + id; | |
Log.d("Activity", "readData:" + readData); | |
} else { | |
Log.d("Activity", "NO_DATA"); | |
}// else | |
db.close(); | |
}// readDatabase | |
/** "send_check"カラムの書き換え */ | |
public void writeDatabase(Context context, String serial){ | |
SQLiteDatabase db = (new sendDatabaseHelper(context)).getWritableDatabase(); | |
String[] target = {serial}; | |
ContentValues cv = new ContentValues(); | |
cv.put("send_check", true); | |
db.update("data", cv, "serial = ?", target); | |
db.close(); | |
}// writeDatabase | |
}// class SendDataService |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment