排球不连接烧瓶服务器
Volley doesnot connect with flask server
我的java代码没有连接到烧瓶。它不会给出任何错误或响应。无限编程 运行。
当我尝试连接时,它没有响应或错误功能。完成函数调用后,它开始 运行ning 并且永不停止。
Java代码:
Button button;
RequestQueue rQueue;
String url ="http://127.0.0.1:5000/predict";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
rQueue = Volley.newRequestQueue(MainActivity.this);
}
public void predict(View view){
UploadTwoImages();
}
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
public void UploadTwoImages() {
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.logo);
final String imageOne = getStringImage(icon);
final ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Registration is in Process Please wait...");
pDialog.show();
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
pDialog.dismiss();
Log.d("JSONResult", s);
try {
JSONObject jObject = new JSONObject(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
//adding parameters to send
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("image", imageOne);
return parameters;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(
90000,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
request.setShouldCache(false);
rQueue.add(request);
}
烧瓶服务器代码:
@app.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == "POST":
if request.files:
output={}
image = request.files["image"]
print(request.files)
basepath = os.path.dirname(__file__)
file_path = os.path.join(
basepath, 'uploads', secure_filename(image.filename))
print(image.filename)
image.save(file_path)
return str([1])
return "Error"
if __name__ == '__main__':
app.run(debug=True)
url = ipv4地址:portNumber/apiName
在 android 清单中添加以下 2 行,它适用于我。
android:usesCleartextTraffic="true"
<uses-permission android:name="android.permission.INTERNET"/>
我的java代码没有连接到烧瓶。它不会给出任何错误或响应。无限编程 运行。 当我尝试连接时,它没有响应或错误功能。完成函数调用后,它开始 运行ning 并且永不停止。
Java代码:
Button button;
RequestQueue rQueue;
String url ="http://127.0.0.1:5000/predict";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
rQueue = Volley.newRequestQueue(MainActivity.this);
}
public void predict(View view){
UploadTwoImages();
}
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
public void UploadTwoImages() {
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.logo);
final String imageOne = getStringImage(icon);
final ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Registration is in Process Please wait...");
pDialog.show();
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
pDialog.dismiss();
Log.d("JSONResult", s);
try {
JSONObject jObject = new JSONObject(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
//adding parameters to send
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("image", imageOne);
return parameters;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(
90000,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
request.setShouldCache(false);
rQueue.add(request);
}
烧瓶服务器代码:
@app.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == "POST":
if request.files:
output={}
image = request.files["image"]
print(request.files)
basepath = os.path.dirname(__file__)
file_path = os.path.join(
basepath, 'uploads', secure_filename(image.filename))
print(image.filename)
image.save(file_path)
return str([1])
return "Error"
if __name__ == '__main__':
app.run(debug=True)
url = ipv4地址:portNumber/apiName
在 android 清单中添加以下 2 行,它适用于我。
android:usesCleartextTraffic="true"
<uses-permission android:name="android.permission.INTERNET"/>