package com.corea.Gdata;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.extensions.Where;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
public class Google_Gdata_Test extends Activity {
/** Called when the activity is first created. */
static String USER_ID;
static String USER_Pass;
static String APP_NAME;
TextView text;
static ArrayList<String> items;
static ArrayList<String> items2;
static ArrayList<String> items3;
JSONArray arr;
String ar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
USER_ID = "지메일 계정";
USER_Pass = "비밀번호";
APP_NAME = "앱이름";
items = new ArrayList<String>();
items2 = new ArrayList<String>();
items3 = new ArrayList<String>();
text = new TextView(this);
text = (TextView)findViewById(R.id.text);
authdata();
if(ar!=null)
{
getList();
}
if(arr.length()!=0)
{
for(int i=0; i<arr.length(); i++)
{
try {
text.append("\n"+arr.getString(i)+"\n");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else{}
}
public void getList()
{
try {
items3=new ArrayList<String>();
URL url = new URL("http://www.google.com/calendar/feeds/"+USER_ID+"/private/full?alt=json");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Authorization", "GoogleLogin auth="+ar);
InputStream un = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(un));
String ud;
arr = new JSONArray();
while((ud=br.readLine())!=null)
{
arr.put(ud);
}
br.close();
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void authdata()
{
items = new ArrayList<String>();
URL url;
try {
url = new URL("https://www.google.com/accounts/ClientLogin ");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
out.write(
"&Email="+USER_ID
// +"&postId="+"1"
//+"&commentId="+"9155368"
+"&Passwd="+USER_Pass
+"&source="+APP_NAME
+"&service="+"cl");
out.flush();
InputStream put = con.getInputStream();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String decodedString;
while((decodedString = in.readLine())!=null)
{
items.add(decodedString);
}
in.close();
ar = items.get(2).substring(5);
System.out.println(ar);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void AddEventSce(String Title, String Note, Date Startdate, Date EndDate, String Location)
{
Toast.makeText(getBaseContext(), "login..", Toast.LENGTH_SHORT).show();
try {
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/private/full?alt=json");
CalendarEventEntry myEntry = new CalendarEventEntry();
myEntry.setTitle(new PlainTextConstruct("Test_App","utf-8"));
myEntry.setContent(new PlainTextConstruct("안녕하세요","utf-8"));
Person author = new Person("Test_app",null,USER_ID);
myEntry.getAuthors().add(author);
DateTime startTime = new DateTime(new Date(2011-1900,12-1,3,12,0));
startTime.setTzShift(9);
DateTime endTime = new DateTime(new Date(2011-1900,12-1,3,13,0));
endTime.setTzShift(9);
When eventTimes = new When();
eventTimes.setStartTime(startTime);
eventTimes.setEndTime(endTime);
myEntry.addTime(eventTimes);
Where evLocaiton = new Where();
evLocaiton.setValueString("서울");
myEntry.addLocation(evLocaiton);
CalendarService myService = new CalendarService("Test_App");
myService.setUserCredentials(USER_ID,USER_Pass);
ContentType ct = new ContentType();
// ct.setType("application/json;charset=utf-8");
// myService.setContentType(ct);
myService.insert(feedUrl, myEntry);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
authdata();
InputStream put = con.getInputStream();
이 두곳에서 에러뜨는데 접속 자체가 안된건가요..? 구글링해도 안 나와 질문드립니다 ㅠㅠ
참고사이트는 이곳입니다.
http://www.androidstudy.co.kr/bbs/board.php?bo_table=B52&wr_id=140&sfl=mb_id%2C1&stx=hecker304
