gpt4 book ai didi

java - Android Retrofit连接JAVA套接字

转载 作者:行者123 更新时间:2023-12-01 06:02:53 26 4
gpt4 key购买 nike

如何使用Retrofit进行Java服务器套接字连接。我的服务器有 JAVA 服务器套接字。并想使用 Retrofit 从 android 发送请求。如何通过 Java 套接字建立连接并发送接收的数据

最佳答案

这是您通过 Retrofit 库连接到 Web 服务的方法。您可以使用此示例来完成您想做的所有事情:

1-首先您需要创建一个通信器类(以执行所有发送-接收过程)

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()));
}
});
}

这两种方法都用于将用户名和密码发送到服务器进行身份验证。

2秒您应该在 Activity 中使用通信器类:

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);
}
}

在onCreate方法中,我们创建了一个Communicator类的实例,并调用useGet和usePost方法将输入的用户名和密码发送到服务器。

3-不要忘记添加库依赖项(将此代码添加到您的 build.gradle(Module:app) 文件中:

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'

关于java - Android Retrofit连接JAVA套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53430530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com