应用程序连接超时,服务器无法处理数据
The App connection is timing out before the server could process the data
我构建的应用程序连接到我在 flask 中构建的本地服务器,但是 python 程序需要时间来执行并且客户端应用程序关闭连接而不使用 return 语句然后转到 catch 块并说 Failed!!!。那么我应该怎么做才能保持连接。我想显示一个处理栏或
**
服务器应在处理完成后通知应用程序
**
我想我什至可以构建另一个按钮来获取处理后的数据(所以我需要通知处理完成)
所以我该怎么做。有人可以详细指导我吗?谢谢。
enter code here''' `
import flask
import werkzeug
import time
from flask import flash
app = flask.Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def handle_request():
files_ids = list(flask.request.files)
print("\nNumber of Received Images : ", len(files_ids))
image_num = 1
for file_id in files_ids:
print("\nSaving Image ", str(image_num), "/", len(files_ids))
imagefile = flask.request.files[file_id]
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("Image Filename : " + imagefile.filename)
timestr = time.strftime("%Y%m%d-%H%M%S")
imagefile.save(timestr + '_' + filename)
image_num = image_num + 1
print("\n")
// flash("Connected")
return "Image(s) Uploaded Successfully. Come Back Soon."
app.run(host="0.0.0.0", port=5000, debug=True)
'''
这是接受传入连接的烧瓶代码。
public void connectServer(View v) {
TextView responseText = findViewById(R.id.responseText);
if (imagesSelected == false) { // This means no image is selected and thus nothing to upload.
responseText.setText("No Image Selected to Upload. Select Image(s) and Try Again.");
return;
}
responseText.setText("Sending the Files. Please Wait ...");
EditText ipv4AddressView = findViewById(R.id.IPAddress);
String ipv4Address = ipv4AddressView.getText().toString();
EditText portNumberView = findViewById(R.id.portNumber);
String portNumber = portNumberView.getText().toString();
Matcher matcher = IP_ADDRESS.matcher(ipv4Address);
if (!matcher.matches()) {
responseText.setText("Invalid IPv4 Address. Please Check Your Inputs.");
return;
}
String postUrl = "http://" + ipv4Address + ":" + portNumber + "/";
MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (int i = 0; i < selectedImagesPaths.size(); i++) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
// Read BitMap by file path.
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagesPaths.get(i), options);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
}catch(Exception e){
responseText.setText("Please Make Sure the Selected File is an Image.");
return;
}
byte[] byteArray = stream.toByteArray();
multipartBodyBuilder.addFormDataPart("image" + i, "Android_Flask_" + i + ".jpg", RequestBody.create(MediaType.parse("image/*jpg"), byteArray));
}
RequestBody postBodyImage = multipartBodyBuilder.build();
postRequest(postUrl, postBodyImage);
}
void postRequest(String postUrl, RequestBody postBody) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
Log.d("FAIL", e.getMessage());
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
responseText.setText("Failed to Connect to Server. Please Try Again.");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
try {
responseText.setText("Server's Response\n" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
'''
这是我的 android 工作室连接服务器代码
要从服务器获取图像到您的应用程序,请使用
Picasso.get()
.load("*The link of your server*")
.into(imageView);
我构建的应用程序连接到我在 flask 中构建的本地服务器,但是 python 程序需要时间来执行并且客户端应用程序关闭连接而不使用 return 语句然后转到 catch 块并说 Failed!!!。那么我应该怎么做才能保持连接。我想显示一个处理栏或
**
服务器应在处理完成后通知应用程序
** 我想我什至可以构建另一个按钮来获取处理后的数据(所以我需要通知处理完成) 所以我该怎么做。有人可以详细指导我吗?谢谢。
enter code here''' `
import flask
import werkzeug
import time
from flask import flash
app = flask.Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def handle_request():
files_ids = list(flask.request.files)
print("\nNumber of Received Images : ", len(files_ids))
image_num = 1
for file_id in files_ids:
print("\nSaving Image ", str(image_num), "/", len(files_ids))
imagefile = flask.request.files[file_id]
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("Image Filename : " + imagefile.filename)
timestr = time.strftime("%Y%m%d-%H%M%S")
imagefile.save(timestr + '_' + filename)
image_num = image_num + 1
print("\n")
// flash("Connected")
return "Image(s) Uploaded Successfully. Come Back Soon."
app.run(host="0.0.0.0", port=5000, debug=True)
''' 这是接受传入连接的烧瓶代码。
public void connectServer(View v) {
TextView responseText = findViewById(R.id.responseText);
if (imagesSelected == false) { // This means no image is selected and thus nothing to upload.
responseText.setText("No Image Selected to Upload. Select Image(s) and Try Again.");
return;
}
responseText.setText("Sending the Files. Please Wait ...");
EditText ipv4AddressView = findViewById(R.id.IPAddress);
String ipv4Address = ipv4AddressView.getText().toString();
EditText portNumberView = findViewById(R.id.portNumber);
String portNumber = portNumberView.getText().toString();
Matcher matcher = IP_ADDRESS.matcher(ipv4Address);
if (!matcher.matches()) {
responseText.setText("Invalid IPv4 Address. Please Check Your Inputs.");
return;
}
String postUrl = "http://" + ipv4Address + ":" + portNumber + "/";
MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (int i = 0; i < selectedImagesPaths.size(); i++) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
// Read BitMap by file path.
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagesPaths.get(i), options);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
}catch(Exception e){
responseText.setText("Please Make Sure the Selected File is an Image.");
return;
}
byte[] byteArray = stream.toByteArray();
multipartBodyBuilder.addFormDataPart("image" + i, "Android_Flask_" + i + ".jpg", RequestBody.create(MediaType.parse("image/*jpg"), byteArray));
}
RequestBody postBodyImage = multipartBodyBuilder.build();
postRequest(postUrl, postBodyImage);
}
void postRequest(String postUrl, RequestBody postBody) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
Log.d("FAIL", e.getMessage());
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
responseText.setText("Failed to Connect to Server. Please Try Again.");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
try {
responseText.setText("Server's Response\n" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
''' 这是我的 android 工作室连接服务器代码
要从服务器获取图像到您的应用程序,请使用
Picasso.get()
.load("*The link of your server*")
.into(imageView);