And again, juices in java androids



  • I'd like to ask you about the sockets in Android. I've been digging half the Internet and I haven't found what I need.

    The challenge is to write a client under Android to Java who would send and receive messages. When writing on the computer, it was wonderful, there was a multi-potential problem. I've been more confused, forced to send messages to the server, but there's no way that textView will take and update the dialogue history.

    Here's the client code, the server even rewrite like a simple echo server, sends back what the client sent. Below the client's code, help with the server reception.

    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;

    public class MainActivity extends AppCompatActivity {

    private Socket socket;
    private Button SendBut;
    private EditText editText;
    private TextView textView;
    private PrintWriter os;
    private BufferedReader is;
    networking net;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SendBut = (Button) findViewById(R.id.SendBut);
        editText = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.TxtFrSrv);
        net = new networking();
        net.execute();
    }
    
    public void onClck(View view) {
        String txt = null;
        txt = editText.getText().toString();
        os.println(txt);
    }
    
    
    public class networking extends AsyncTask < String, Void, Void > {
        @Override
        protected Void doInBackground(String...params) {
            try {
                socket = new Socket("10.0.2.2", 4444);
                os = new PrintWriter(new PrintWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    

    }

    I tried to paint something like that, but it doesn't work at zero. ♪ ♪

    public class getMsg extends Thread{
    public void run(){
    try {
    is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
    final String finc;
    finc=is.readLine();
    while (!finc.equals("break")){
    MainActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
    textView.setText(finc);
    }
    });
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }



  • Use retrofit Luke!

    Retrofit+rxJava+retrolambda

    Let's say the answer from the server is like this.

    {"text":"текст сообщения"}
    

    How

    public class Message{
     @SerializedName("text") public String message;
    }
    

    public interface RequestApi {
    String BASE_URL = "http://server/";
    RequestApi api = new Retrofit.Builder()
    .baseUrl(RequestApi.BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .subscribeOn(AndroidSchedulers.mainThread()
    .build()
    .create(RequestApi.class);
    @GET("?method=getMsg")
    Observable<Message> getMessage();
    }

    public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SendBut = (Button) findViewById(R.id.SendBut);
    editText = (EditText) findViewById(R.id.editText);
    textView = (TextView) findViewById(R.id.TxtFrSrv);
    RequestApi.api.getMessage().subscribe(m->textView.setText(m.message));
    }
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2