gpt4 book ai didi

java - 用于访问服务器的单个可运行程序,由不同类型的请求子类: how to ensure uniqueness?

转载 作者:行者123 更新时间:2023-12-02 10:21:06 25 4
gpt4 key购买 nike

这是一个负责与服务器通信的类:

public abstract class AbstractCommunicationChannel implements Runnable {
static String SERVER_ADDRESS = "http://0.0.0.0";

private URL url;
private JSONObject requestObject;
private JSONObject responseObject;

AbstractCommunicationChannel(URL url, JSONObject requestObject) {
this.url = url;
this.requestObject = requestObject;
}

/**
* This is the general purpose tool for hitting the server and getting a response back.
*/
public void run() {
Log.i("requestObject", requestObject.toString());
try {
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpUrlConnection.getOutputStream());
outputStreamWriter.write(requestObject.toString());
outputStreamWriter.flush();
outputStreamWriter.close();
/* * */
InputStream inputStream = httpUrlConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int result = bufferedInputStream.read();
while (result != -1) {
byteArrayOutputStream.write((byte) result);
result = bufferedInputStream.read();
}
responseObject = new JSONObject(byteArrayOutputStream.toString("UTF-8"));
httpUrlConnection.disconnect();
} catch (Exception ignored) {
}
processResponse(responseObject);
}

protected abstract void processResponse(JSONObject responseObject);
}

这是一个示例子类,负责特定类型的请求:

public class LoginRequester extends AbstractCommunicationChannel {

public LoginRequester(String username, String password) throws Exception {
super(new URL(SERVER_ADDRESS + ":0000/login"),
new JSONObject().put("username", username).put("password", password));
}

@Override
protected void processResponse(JSONObject responseObject) {
try {
if (responseObject.get("result").equals("valid")) {
StartActivity.accessAccountActivity();
}

if (responseObject.get("result").equals("username")) {
Toast.makeText(StartActivity.startContext, "No such username exists!", Toast.LENGTH_LONG).show();
}
if (responseObject.get("result").equals("password")) {
Toast.makeText(StartActivity.startContext, "Invalid password!", Toast.LENGTH_LONG).show();
}
} catch (Exception ignored) {
}
}
}

对于上下文,这是另一个:

public class CreateRequester extends AbstractCommunicationChannel {

public CreateRequester(String username, String password) throws Exception {
super(new URL(SERVER_ADDRESS + ":8080/create"),
new JSONObject().put("username", username).put("password", password));
}

@Override
protected void processResponse(JSONObject responseObject) {
try {
if (responseObject.get("result").equals("success")) {
StartActivity.accessAccountActivity();

} else {

Toast.makeText(StartActivity.startContext, "ERROR!", Toast.LENGTH_LONG).show();
}
} catch (Exception ignored) {
}
}
}

我想要的是只有一个实体能够向服务器发出请求,因此是否有某种方法可以使 AbstractCommunicationChannel 同步以确保不会有两个或多个线程同时与服务器通信?

最佳答案

您可以在抽象类中使用类级锁(如果您使用单个类加载器):

类级锁可防止多个线程在运行时进入该类的任何可用实例中的同步块(synchronized block)。

例如:

public abstract class AbstractCommunicationChannel implements Runnable {
private static Object lock = new Object();

public void run() {
synchronized(lock){
Log.i("requestObject", requestObject.toString());
try {
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpUrlConnection.getOutputStream());
outputStreamWriter.write(requestObject.toString());
outputStreamWriter.flush();
outputStreamWriter.close();
/* * */
InputStream inputStream = httpUrlConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int result = bufferedInputStream.read();
while (result != -1) {
byteArrayOutputStream.write((byte) result);
result = bufferedInputStream.read();
}
responseObject = new JSONObject(byteArrayOutputStream.toString("UTF-8"));
httpUrlConnection.disconnect();
} catch (Exception ignored) {
}
}
processResponse(responseObject);
}

关于java - 用于访问服务器的单个可运行程序,由不同类型的请求子类: how to ensure uniqueness?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54337314/

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