Android改造连接JAVA套接字
如何使用Java服务器套接字连接的改装。 我的服务器有JAVA服务器套接字。 并希望通过改型从android发送请求。Android改造连接JAVA套接字,java,android,sockets,server,retrofit,Java,Android,Sockets,Server,Retrofit,如何使用Java服务器套接字连接的改装。 我的服务器有JAVA服务器套接字。 并希望通过改型从android发送请求。 如何通过Java套接字建立连接并发送接收到的数据这里是通过改装库连接到web服务的方法。您可以使用此示例执行您想要执行的所有操作: 1-首先需要创建通信器类(执行所有发送-接收过程) 在onCreate方法中,我们创建了communicator类的实例,并调用useGet和usePost方法将输入的用户名和密码发送到服务器 3-不要忘记添加库依赖项(将这些代码添加到build.
如何通过Java套接字建立连接并发送接收到的数据这里是通过改装库连接到web服务的方法。您可以使用此示例执行您想要执行的所有操作: 1-首先需要创建通信器类(执行所有发送-接收过程) 在onCreate方法中,我们创建了communicator类的实例,并调用useGet和usePost方法将输入的用户名和密码发送到服务器 3-不要忘记添加库依赖项(将这些代码添加到build.gradle(模块:app)文件中):
改装不是为套接字而设计的。您确定此实现用于提供套接字编程吗?不,改装仅用于普通http连接。对于套接字连接,我强烈建议使用socket.io客户端。您可以在此处找到更多信息:因此您的答案不适用于此答案,并且它是错误的
public class Communicator {
private static final String TAG = "Communicator";
private static final String SERVER_URL = "http://127.0.0.1/retrofit";
public void loginPost(String username, String password){
//Here a logging interceptor is created
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
//The logging interceptor will be added to the http client
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
//The Retrofit builder will have the client attached, in order to get connection logs
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(SERVER_URL)
.build(); Interface service = retrofit.create(Interface.class);
Call<ServerResponse> call = service.post("login",username,password);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
BusProvider.getInstance().post(new ServerEvent(response.body()));
Log.e(TAG,"Success");
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
// handle execution failures like no internet connectivity
BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
}
});
}
public void loginGet(String username, String password){
//Here a logging interceptor is created
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
//The logging interceptor will be added to the http client
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
//The Retrofit builder will have the client attached, in order to get connection logs
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(SERVER_URL)
.build();
Interface service = retrofit.create(Interface.class);
Call<ServerResponse> call = service.get("login",username,password);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
BusProvider.getInstance().post(new ServerEvent(response.body()));
Log.e(TAG,"Success");
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
// handle execution failures like no internet connectivity
BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
}
});
}
public class MainActivity extends AppCompatActivity {
private Communicator communicator;
private String username, password;
private EditText usernameET, passwordET;
private Button loginButtonPost, loginButtonGet;
private TextView information, extraInformation;
private final static String TAG = "MainActivity";
public static Bus bus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
communicator = new Communicator();
usernameET = (EditText)findViewById(R.id.usernameInput);
passwordET = (EditText)findViewById(R.id.passwordInput);
//This is used to hide the password's EditText characters. So we can avoid the different hint font.
passwordET.setTransformationMethod(new PasswordTransformationMethod());
loginButtonPost = (Button)findViewById(R.id.loginButtonPost);
loginButtonPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = usernameET.getText().toString();
password = passwordET.getText().toString();
usePost(username, password);
}
});
loginButtonGet = (Button)findViewById(R.id.loginButtonGet);
loginButtonGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = usernameET.getText().toString();
password = passwordET.getText().toString();
useGet(username, password);
}
});
information = (TextView)findViewById(R.id.information);
extraInformation = (TextView)findViewById(R.id.extraInformation);
}
private void usePost(String username, String password){
communicator.loginPost(username, password);
}
private void useGet(String username, String password){
communicator.loginGet(username, password);
}
}
compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup:otto:1.3.8'
compile 'com.google.code.gson:gson:2.6.2'