package com.example.at;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView txtResponse;
private EditText edtTextAddress, edtTextPort;
private Button btnConnect, btnClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtTextAddress = (EditText) findViewById(R.id.address);
edtTextPort = (EditText) findViewById(R.id.port);
btnConnect = (Button) findViewById(R.id.connect);
btnClear = (Button) findViewById(R.id.clear);
txtResponse = (TextView) findViewById(R.id.response);
btnConnect.setOnClickListener(buttonConnectOnClickListener);
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtResponse.setText("");
}
});
}
// 클릭이벤트 리스너
OnClickListener buttonConnectOnClickListener = new OnClickListener() {
public void onClick(View arg0) {
NetworkTask myClientTask = new NetworkTask(
edtTextAddress.getText().toString(),
Integer.parseInt(edtTextPort.getText().toString())
);
myClientTask.execute();
}
};
public class NetworkTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response;
NetworkTask(String addr, int port) {
dstAddress = addr;
dstPort = port;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
Socket socket = new Socket(dstAddress, dstPort);
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
1024);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
socket.close();
response = byteArrayOutputStream.toString("UTF-8");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
txtResponse.setText(response);
super.onPostExecute(result);
}
}
}
'안드로이드' 카테고리의 다른 글
안드로이드 다중 서버클라이언트(xml부분) (0) | 2015.05.27 |
---|---|
안드로이드 다중 서버클라이언트(서버부분) (0) | 2015.05.27 |
웹뷰 셋팅과 뒤로가기이벤트 (0) | 2015.05.15 |
탭호스트 사용법 (0) | 2015.05.15 |
이벤트처리:어떤 리스너들이 있는가? (0) | 2015.03.13 |