Created
January 26, 2016 06:40
-
-
Save a1yama/0e586f1479f9f0c922ea to your computer and use it in GitHub Desktop.
This file contains 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
import android.os.AsyncTask; | |
import android.util.Base64; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class AsyncJob extends AsyncTask<String,String,String> { | |
private MainActivity _main; | |
public AsyncJob(MainActivity main) { | |
super(); | |
_main = main; | |
} | |
@Override | |
protected String doInBackground(String...value) { | |
String userToken = value[0]; | |
String devicetype = value[1]; | |
String regId = value[2]; | |
String base_url = value[3]; | |
BufferedReader reader = null; | |
OutputStream os = null; | |
HttpURLConnection urlCon = null; | |
try { | |
URL url = new URL(base_url); | |
urlCon = (HttpURLConnection) url.openConnection(); | |
urlCon.setReadTimeout(10000); | |
urlCon.setConnectTimeout(20000); | |
urlCon.setRequestMethod("POST"); | |
urlCon.setDoInput(true); | |
urlCon.setDoOutput(true); | |
urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
urlCon.setUseCaches(false); | |
urlCon.connect(); | |
JSONObject jsonObject = new JSONObject(); | |
jsonObject.put("token", userToken); | |
jsonObject.put("devicetype", devicetype); | |
jsonObject.put("deviceid", regId); | |
// データを送信する | |
os = urlCon.getOutputStream(); | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); | |
writer.write(String.valueOf(jsonObject)); | |
writer.flush(); | |
writer.close(); | |
os.close(); | |
int status = urlCon.getResponseCode(); | |
switch (status) { | |
case HttpURLConnection.HTTP_OK: | |
InputStream is = urlCon.getInputStream(); | |
reader = new BufferedReader(new InputStreamReader(is)); | |
String httpSource = new String(); | |
String str; | |
while (null != (str = reader.readLine())) { | |
httpSource = httpSource + str; | |
} | |
is.close(); | |
break; | |
case HttpURLConnection.HTTP_UNAUTHORIZED: | |
break; | |
default: | |
break; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (reader != null) { | |
reader.close(); | |
} | |
if (os != null) { | |
os.close(); | |
} | |
if (urlCon != null) { | |
urlCon.disconnect(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return null; | |
} | |
@Override | |
protected void onProgressUpdate(String... values) { | |
// | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
_main.result_job(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment