这里是指负责与服务器通信的一类:
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
同步,以确保不能有两个或更多,螺纹谈话的服务器在任何一个时间?
您可以在抽象类中使用类级锁(如果使用单一的类装载器):
类级锁可以防止多个线程以同步块中的任何运行时类的所有可用实例的进入。
例如:
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);
}