トップページ > プログラム > 2014年05月28日 > AMz7q1yT

書き込み順位&時間帯一覧

15 位/195 ID中時間01234567891011121314151617181920212223Total
書き込み数0040000000000000000000004



使用した名前一覧書き込んだスレッド一覧
デフォルトの名無しさん
Androidプログラミング質問スレ revision43

書き込みレス一覧

Androidプログラミング質問スレ revision43
104 :デフォルトの名無しさん[sage]:2014/05/28(水) 02:24:42.82 ID:AMz7q1yT
>>102
まあ無理だろうね。BufferedInputStreamなんかこの件とは全く無関係だし。
Socketのことわかってないのに、なぜSocketでやろうとするの?
SocketがわかっていないのならURL.openStream()でも使うべき。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

package com.example.testsocket;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

public class MainActivity extends Activity {
Androidプログラミング質問スレ revision43
105 :デフォルトの名無しさん[sage]:2014/05/28(水) 02:25:17.61 ID:AMz7q1yT
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final String path = getExternalFilesDir(null).getAbsolutePath()+ File.separator + "2ch-logo.gif";
final Handler handler = new Handler();
final Context context = this;

new Thread(new Runnable() {
@Override
public void run() {
// http://www.2ch.net/2ch-logo.gif の取得
Socket sock = null;
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
sock = new Socket("www.2ch.net", 80);
in = new BufferedInputStream(sock.getInputStream());
sock.getOutputStream().write("GET /2ch-logo.gif HTTP/1.1\nHost: www.2ch.net\n\n".getBytes());
out = new BufferedOutputStream(new FileOutputStream(path));
int len;
int bytes = 0;
while ((len = in.read()) != -1) {
// このifの条件節は良い子は真似しちゃ駄目。このサンプルでしか通用しない数値。
if (bytes++ > 247) {
out.write(len);
}
}
out.flush();
Androidプログラミング質問スレ revision43
106 :デフォルトの名無しさん[sage]:2014/05/28(水) 02:25:54.86 ID:AMz7q1yT
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sock != null) {
try {
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Androidプログラミング質問スレ revision43
107 :デフォルトの名無しさん[sage]:2014/05/28(水) 02:26:22.28 ID:AMz7q1yT
handler.post(new Runnable() {
@Override
public void run() {
ImageView imageView = new ImageView(context);
imageView.setImageURI(Uri.parse(path));
setContentView(imageView);
}
});
}
}}).start();
}
}


※このページは、『2ちゃんねる』の書き込みを基に自動生成したものです。オリジナルはリンク先の2ちゃんねるの書き込みです。
※このサイトでオリジナルの書き込みについては対応できません。
※何か問題のある場合はメールをしてください。対応します。