salut

bon j' entrain de développe une application client serveur (serveur java/ client android)
donc je maître en place un service qui doit établie connexion avec le serveur
tous marche bien
problème que je utilise les méthodes de service pour envoyer au receive des message de serveur

donc je lance le service a partir de MainActivity mais j'arrive pas a envoyé un message au serveur
merci d'avance

1- service
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.example.yaine.myae;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
 
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
 
public class SocketService extends Service {
    public static final String SERVERIP = "192.168.1.7"; //your computer IP address should be written here
    public static final int SERVERPORT = 4444;
    PrintWriter out;
    Socket socket;
    InetAddress serverAddr;
     BufferedReader in;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("I am in Ibinder onBind method");
        return myBinder;
    }
 
    private final IBinder myBinder = new LocalBinder();
    MainActivity mTcpClient = new MainActivity();
 
    public class LocalBinder extends Binder {
        public SocketService getService() {
            System.out.println("I am in Localbinder ");
            return SocketService.this;
 
        }
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("I am in on create");
    }
 
    public void IsBoundable(){
        Toast.makeText(this,"I bind like butter", Toast.LENGTH_LONG).show();
    }
 
    public void sendMessage(String message){
        if (out != null && !out.checkError()) {
            System.out.println("in sendMessage"+message);
            out.println(message);
            out.flush();
        }
    }
    public String reciveMessage() throws IOException {
        String m="";
        if (in.ready()) {
 
            m= in.readLine();
        }
        return m;
    }
    @Override
    public int onStartCommand(Intent intent,int flags, int startId){
        super.onStartCommand(intent, flags, startId);
        System.out.println("I am in on start");
        //  Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
        Runnable connect = new connectSocket();
        new Thread(connect).start();
        return START_STICKY;
    }
 
 
    class connectSocket implements Runnable {
 
        @Override
        public void run() {
 
 
            try {
                //here you must put your computer's IP address.
                serverAddr = InetAddress.getByName(SERVERIP);
                Log.e("TCP Client", "C: Connecting...");
                //create a socket to make the connection with the server
 
                socket = new Socket(SERVERIP, SERVERPORT);
 
                try {
 
 
                    //send the message to the server
                    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
 
 
                    Log.e("TCP Client", "C: Sent.");
 
                    Log.e("TCP Client", "C: Done.");
 
 
                }
                catch (Exception e) {
 
                    Log.e("TCP", "S: Error", e);
 
                }
            } catch (Exception e) {
 
                Log.e("TCP", "C: Error", e);
 
            }
 
        }
 
    }
 
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            socket.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        socket = null;
    }
 
 
}
2- Main activity

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.example.yaine.myae;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
 
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.os.IBinder;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;
/**
 * This is the main activity class for Client-Server Chat App.
 *
 * @author Lak J Comspace (http://lakjeewa.blogspot.com)
 *
 */
public class MainActivity extends Activity {
 
    private EditText textField;
    private Button button;
    private EditText textField1;
    private Socket client;
    private PrintWriter printwriter;
    private BufferedReader bufferedReader;
    private String message, message1,message2;
    private boolean mIsBound;
    public SocketService  mBoundService;
    //Following is the IP address of the chat server. You can change this IP address according to your configuration.
    // I have localhost IP address for Android emulator.
    private String CHAT_SERVER_IP = "192.168.1.7";
    private ServiceConnection mConnection = new ServiceConnection() {
 
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
 
            mBoundService = ((SocketService.LocalBinder)service).getService();
 
        }
 
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            mBoundService = null;
        }
 
    };
    private void doBindService() {
        bindService(new Intent(MainActivity.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
        if(mBoundService!=null){
            mBoundService.IsBoundable();
        }
    }
 
    private void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.activity_main);
        startService(new Intent(MainActivity.this,SocketService.class));
        doBindService();
        authentification();
 
    }
    public void authentification() {
        if(mBoundService!=null){
 
 
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
 
                    message = textField.getText().toString();
                    message1 = textField1.getText().toString();
                    Log.e("TCP Client", "get info ...");
                    mBoundService.sendMessage(message);
                    mBoundService.sendMessage(message1);
                    Log.e("TCP Client", "send etat ...");
                }
            });
            while (true) {
 
 
                try {
                    String etat= mBoundService.reciveMessage();
                    if (etat.equals("true")) {
                        Intent myIntent = new Intent(MainActivity.this, Acc.class);
                        //Optional parameters
                        MainActivity.this.startActivity(myIntent);
                        Log.e("dddd", "nta essskssl");
                    } else {
 
                        Log.e("dddd", "nnnnnnnnnnnnn");
                    }
 
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
        } else {
 
            Log.d("dddd", "Server has not bean started on port 4444.");
        }
 
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }
 
}
2- une autre activite
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.example.yaine.myae;
 
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
 
/**
 * Created by yaine on 27/04/2018.
 */
 
public class Acc extends Activity {
    private EditText textField;
    private Button button;
    private EditText textField1;
    private Socket client;
    private PrintWriter printwriter;
    private BufferedReader bufferedReader;
    private String message, message1, message2;
 
 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textField = (EditText) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
 
 
    }
 
 /*   public void authentification() {
        if (mBoundService != null) {
 
 
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
 
                    message = textField.getText().toString();
                    message1 = textField1.getText().toString();
                    Log.d("dddd", "zazaza");
                    mBoundService.sendMessage(message);
                    mBoundService.sendMessage(message1);
 
 
                    Log.d("dddd", " Sender");
 
                }
            });
            while (true) {
 
 
                try {
                    String etat = mBoundService.reciveMessage();
                    if (etat.equals("true")) {
                        Intent myIntent = new Intent(Acc.this, Acc.class);
                        //Optional parameters
                        Acc.this.startActivity(myIntent);
                        Log.e("dddd", "nta essskssl");
                    } else {
 
                        Log.e("dddd", "nnnnnnnnnnnnn");
                    }
 
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
        } else {
 
            Log.d("dddd", "Server has not bean started on port 4444.");
        }
 
 
    }*/
}