우선 코드는 맨처음에 thread를 만들어서 서버를 실행시키고 그 thread안에서 다시 thread를 만들어서
서버로부터 스트림을 readobject하는데요..
여기서 제가 하려고하는건 클라이언트에서 서버로 데이터를 보내면 그동안에는 loading창을
progressdialog 띄워서 잠시 기다렸다가 서버로부터 다시 readobject가 실행되면
그때 dialog창을 dismiss하고 이후 내용을 실행시키고 싶습니다.
일단 여러군대 activity에서 실행하게 만들기위해서 메소드에 context를 생성자로 넣었는데 .. 뭐가 문젠지
이런 오류가 뜨네요 ㅠㅠ
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
도와주세요
public void send_toServer(SendObject so,Context context) {//Serializable
try {
oos.writeObject(so);
oos.flush();
oos.reset();
wait_Task task = new wait_Task(context);
task.execute();
} catch (NullPointerException e) {
Toast.makeText(this, "서버가 열려있지 않습니다", Toast.LENGTH_LONG).show();
} catch (IOException e1) {
System.out.println("Send_toServer Object영역");
e1.printStackTrace();
}
}
private class wait_Task extends AsyncTask<Void,Void,Void >{
ProgressDialog dialog;
wait_Task(Context context){
dialog = new ProgressDialog(context) ;
}
@Override
protected void onPreExecute() {
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Wait Loading...");
dialog.setMessage("조금만 기다려주세요 ㅠ.ㅠ");
dialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
while(!isreceived){
}
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
super.onPostExecute(result);
}
}
// private class wait_protocol {
// wait_protocol(Context context){
// isreceived = false;
// final Handler handler_send = new Handler(Looper.getMainLooper());
//
// dialog = new ProgressDialog(context);
// new Handler(){
//
// };
//
// Thread thread = new Thread(new Runnable() {
// @Override
// public void run() {
// handler_send.post(new Runnable() {
// @Override
// public void run() {
// dialog.show();
// }
// });
// while(!isreceived){}
// dialog.dismiss();
// }
// });
// thread.start();
//
// }
// }
class ConnectTread extends Thread {
public void run() {
Myclient client = new Myclient();
client.connection();
}
}
public void println(final String msg){
handler.post(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}catch(NullPointerException e){
System.out.println("서버 닫혀있음");
}
}
});
}
private void servercheck(){
handler.post(new Runnable() {
@Override
public void run() {
textview_serverstate.setText("made by 혜윰 , Server Open");
}
});
}
private class Myclient {
String ip = "218.151.105.235";
int port = 5001;
private void connection() {
try {
socket = new Socket(ip, port);
if(socket != null){//Server state check
servercheck();
}
ois = new ObjectInputStream(socket.getInputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
Thread th = new Thread(new Runnable() {
@Override
public void run() { // 서버로부터 계속해서 메시지 받기 대기.
try {
while(true) {
so = (SendObject)ois.readObject();
isreceived = true;
System.out.println("서버로 부터 받은 메시지 : " +so.msg);
new Client_Protocol(so.msg);
}
}catch(ClassCastException e){
e.printStackTrace();
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch (EOFException e) {
e.printStackTrace();
}catch(SocketException e){
println("서버가 열려있지 않습니다.");
try {
oos.close();
ois.close();
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch(IOException e) {
try {
oos.close();
ois.close();
socket.close();
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
th.start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}//connection 메서드 end
}//Myclient class end