- (void)viewDidLoad
{
[super viewDidLoad];
//リクエストを作成
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:@"http://apple.ios.sample.com"]];
[request setTimeoutInterval:30.0];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
//Cookieを保持している場合は読み込む
//TODO Cookieの扱いがおかしい?
NSHTTPCookie *cookie = nil;
NSData *cookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"cookie"];
if (cookiesData.length != 0) {
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesData];
for (cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
} else {
//TODO Cookieが保持されていない
}
NSArray *cookieArray = [NSArray arrayWithObjects: cookie, nil];
NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
[request setAllHTTPHeaderFields:headers];
//ネットワーク接続を開始
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection release];
[request release];
}
#pragma mark -
//サーバーから認証の要求があった時に呼び出される
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
}
//サーバーからレスポンスがあった時に呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//Cookieを保存
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
[[NSUserDefaults standardUserDefaults] setObject:cookiesData
forKey:@"cookie"];
//レスポンスコード取得
statusCode = [((NSHTTPURLResponse *)response) statusCode];
if ([receivedData retainCount]) {
[receivedData release];
}
receivedData = [[NSMutableData alloc] init];
}
//サーバーからデータを受け取ると呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
//サーバーからの通信が終わる直前に呼び出される
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//TODO receivedDataにbodyが入っている
}
2013年5月30日木曜日
[iOS]NSURLConnectionを使用してHTTP接続をする。
cookieの保存が必要だったのでメモを残しておく。
2013年5月14日火曜日
[iOS]Interface BuilderでUIScrollViewのContentSizeを設定することはできない。
Interface BuilderでUIScrollViewのContentSizeを設定使用と思ったが、
それを行うUIが無いことに気づいた。
http://stackoverflow.com/questions/1135163/how-do-i-use-uiscrollview-in-interface-builder
によると、ContentSizeはコード状態のみ設定可能のようだ、IBでやるのは諦めよう。
ただしiOS6のAuto Layoutを使用すれば可能になる。
With Autolayout (iOS6+), you can avoid setting contentSize. Set the following constraints instead:
Pin the top of the scrollview to the top of its top-most child.
And pin the bottom to the bottom of its bottom-most child.
2013年4月27日土曜日
[AndroidSDK]ServiceのbindService()でfalseが帰る
Intent intent = new Intent(this, MyService.class); boolean result = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);.
でfalseが返ってくる
StackOverflowによると
Android Context.bindService always returns false and ServiceConnection object is never triggered
(bindServiceでfalseが返ってきてServiceConnectionが動かねえよ)
getApplicationContext().bindService()でいけるぜ、
この回答で動いた人がおおいみたいだが、今回はどうも違うようだ。
結論
Manifest.xmlで
<service android:name=".MyService" />
となっていたが、パッケージが間違っていた
<service android:name="Utility.MyService" />
2013年4月26日金曜日
[AndroidSDK]Google PlayにおけるAndroid端末のシェア率を見る
公式が出している。
Dashboards
http://developer.android.com/intl/ja/about/dashboards/index.html#Platform
14日前のデータとのこと。
これを見てどのバージョンに合わせるか考えると良い
iOSもこの表があると良いな、不定期にしか出してくれないからな
Dashboards
http://developer.android.com/intl/ja/about/dashboards/index.html#Platform
14日前のデータとのこと。
これを見てどのバージョンに合わせるか考えると良い
iOSもこの表があると良いな、不定期にしか出してくれないからな
2013年4月24日水曜日
[AndroidSDK]AsyncTaskを使用してバックグランド処理を行う。
ネットワーク処理をさせている間にProgressDialogを表示させたい時など
public class MainActivity extends Activity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(this);
MyAsyncTask task = new MyAsyncTask();
//バックグランド処理を実行
task.execute(getApplicationContext());
//このログが実行されているときに、MyAsyncTaskが終わっているとは限らなので
//MyAsyncTask後に処理させたいときはonPostExecute()を使用すること
Log.i("tag", "mes");
}
public class MyAsyncTask extends AsyncTask {
@Override
//doInBackgroundが実行されてる前に呼ばれる
protected void onPreExecute() {
//ProgressDialogなどはここで実装すると良い
progressDialog.setMessage("データを取得しています。");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.show();
}
//バックグランド処理はここに追加
@Override
protected Integer doInBackground(Context... context) {
//何かの処理
return 1;
}
//doInBackgroundが実行された後に呼ばれる
@Override
protected void onPostExecute(Integer result) {
progressDialog.dismiss();
}
}
}
2013年4月23日火曜日
[AndroidSDK]DefaultHttpClientを使ってPOSTをする
public void httpConnect() {
DefaultHttpClient client = new DefaultHttpClient();
StringBuilder uri = new StringBuilder("hostname.com");
HttpPost http = new HttpPost(uri.toString());
http.setHeader("Content-type", "text/plain; charset=utf-8");
//bodyの作成
try {
http.setEntity(new StringEntity("body", "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpResponse httpResponse = null;
try {
// HTTPリクエストを送信
httpResponse = client.execute(http);
} catch (Exception e) {
e.printStackTrace();
}
// HTTPレスポンスを取得
int status = httpResponse.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK == status) {
//ヘッダーの表示
Header[] headerArray = http.getAllHeaders();
for (Header header : headerArray) {
Log.d(getLocalClassName(), "header[" + header.getName() + "] [" + header.getValue() + "]");
}
// Bodyの取得
HttpEntity httpEntity = httpResponse.getEntity();
String body = null;
try {
body = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(getLocalClassName(), "body " + body);
} else {
//TODO エラー処理
}
}
2013年4月22日月曜日
[Android SDK]Activity、アプリを終了させる。
・finish()
でactivityを1つずつ終わらせるのがベスト
ただしスタックされたn個のActivityを終わらせるには、見た目上うまく表示する必要がある。
・this.moveTaskToBack(true);
でアプリがバックグランドに入るので見た目上はこっちの方が良い
・System.exit(int)
は問題があるので使わない方がいい
登録:
投稿 (Atom)