ソケットを介してサーバーに接続するこのクラスがありますが、何らかの理由で常にここからタイムアウトし、理由がわかりません。最初は、onCreate() を使用していることに関係していると思っていました。 () も存在します。任意の助けをいただければ幸いです。これが私のコードです...
public class Ads extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ads);
doit();
};
public void doit(){
Socket socket = null;
FileOutputStream fos = null;
DataInputStream dis = null;
BufferedOutputStream buf = null;
DataOutputStream dos = null;
try {
socket = new Socket("192.168.1.106", 4447);
Bundle extras = getIntent().getExtras();
String value = extras.getString("keyName");
dos = new DataOutputStream(
new BufferedOutputStream(socket.getOutputStream()));
dis = new DataInputStream(new BufferedInputStream(
socket.getInputStream()));
//dos.writeChars(value);
int numFiles = dis.readInt();
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() +value);
dir.mkdirs();
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
new File(dir, children[i]).delete();
}
}
int n = 0;
int fileLength = 0;
for (int i=0;i<numFiles;i++){
File file = new File(dir, String.valueOf(i)+".png");
Log.d("debug tag","created file "+file);
}
for (int i=0;i<numFiles;i++){
fileLength = dis.readInt();
byte[] temp = new byte[(int) fileLength];
String path = sdCard.getAbsolutePath()+value+"/"+i+".png";
buf = new BufferedOutputStream(new FileOutputStream(path));
while ((fileLength > 0) && (n = dis.read(temp, 0, (int) Math.min(temp.length, fileLength))) != -1) {
buf.write(temp,0,n);
buf.flush();
fileLength -= n;
}
//buf.close();
Log.d("debug tag","the file is "+temp.length+" bytes long");
}
// now read in text files
n = 0;
fileLength = 0;
for (int i=0;i<numFiles;i++){
File file = new File(dir, String.valueOf(i)+".txt");
Log.d("debug tag","created file "+file);
}
for (int i=0;i<numFiles;i++){
fileLength = dis.readInt();
byte[] temp = new byte[(int) fileLength];
String path = sdCard.getAbsolutePath()+value+"/"+i+".txt";
buf = new BufferedOutputStream(new FileOutputStream(path));
while ((fileLength > 0) && (n = dis.read(temp, 0, (int) Math.min(temp.length, fileLength))) != -1) {
buf.write(temp,0,n);
buf.flush();
fileLength -= n;
}
//buf.close();
Log.d("debug tag","the text file is "+temp.length+" bytes long");
}
generateListView(sdCard.getAbsoluteFile()+value+"/");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dis != null){
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dos != null){
try {
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
;