package com.example.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.google.android.gcm.GCMRegistrar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
AsyncTask<?, ?, ?> regIDInsertTask;
TextView message;
ProgressDialog loagindDialog;
String myResult;
String regId;
Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
message = (TextView) findViewById(R.id.re_message);
if (GCMIntentService.re_message != null) {
message.setText(GCMIntentService.re_message);
} else {
registerGcm();
Log.e("TEST", "registerGcm");
}
}
public void registerGcm() {
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
Log.e("TEST", "checkDevice, Manifest");
regId = GCMRegistrar.getRegistrationId(this);
Log.e("TEST11", regId);
if (regId.equals("")) {
GCMRegistrar.register(this, "853396760635");
} else {
Log.e("reg_id", regId);
}
sendAPIkey();
}
private void sendAPIkey() {
String myNum = "테스트";
regIDInsertTask = new regIDInsertTask().execute(regId, myNum);
}
private class regIDInsertTask extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
loagindDialog = ProgressDialog.show(MainActivity.this,
"키 등록 중입니다..", "Please wait..", true, false);
}
@Override
protected Void doInBackground(String... params) {
HttpPostData(params[0], params[1]);
return null;
}
protected void onPostExecute(Void result) {
loagindDialog.dismiss();
}
}
public void HttpPostData(String reg_id, String pnum) {
try {
URL url = new URL("http://210.118.34.43/gcm_reg_insert.php"); // URL 설정
HttpURLConnection http = (HttpURLConnection) url.openConnection(); // 접속
// --------------------------
// 전송 모드 설정 - 기본적인 설정이다
// --------------------------
http.setDefaultUseCaches(false);
http.setDoInput(true);
http.setDoOutput(true);
http.setRequestMethod("POST");
http.setRequestProperty("content-type",
"application/x-www-form-urlencoded");
StringBuffer buffer = new StringBuffer();
buffer.append("reg_id").append("=").append(reg_id).append("&"); // php
// 변수에
// 값
// 대입
buffer.append("pnum").append("=").append(pnum);
OutputStreamWriter outStream = new OutputStreamWriter(
http.getOutputStream(), "EUC-KR");
PrintWriter writer = new PrintWriter(outStream);
writer.write(buffer.toString());
writer.flush();
InputStreamReader tmp = new InputStreamReader(
http.getInputStream(), "EUC-KR");
BufferedReader reader = new BufferedReader(tmp);
StringBuilder builder = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) {
builder.append(str + "\n");
}
myResult = builder.toString();
} catch (MalformedURLException e) {
//
} catch (IOException e) {
//
} // try
} // HttpPostData
}
사용자 Resist값을 php 서버로 보내는 예제인데 분석중 이해가 되지 않아서 질문드립니다.
최초 실행시 한번만 서버에 등록하고 그다음 실행부터는 등록되지 않게 하고싶은데..
어플이 실행될때마다 php서버로 값을 보냅니다..
어떻게 해결해야할까요?