改装在第一个

时间:2019-07-15 14:00:50

标签: java android rest flask retrofit

我正在Android上使用RetroFit,以便将图像发送到REST服务器(使用Python和Flask开发)。该应用程序将第一张图像发送到存储该图像的服务器,并将该图像的ID返回给客户端。然后,客户端拍摄另一张照片并将其发送到服务器,这次是在端点URL中使用先前的图像ID。然后,服务器应计算从图像A到图像B的旋转并返回结果。客户端发送完第二张图像后,它应重复轮询端点以获取结果,直到服务器端计算完成。

第一个图像正确发送,服务器将其存储。客户端然后正确地从服务器接收响应。当客户端发送第二张图像时,会发生此问题。服务器正确接收了POST,但是客户端似乎从未收到响应。对于第三条消息(轮询结果的GET请求)也是如此,服务器已接收到该消息,但客户端从未看到响应。

我的REST服务器的代码在这里

class Image(Resource):
    def post(self):
        file = request.files['uploaded_file']
        centerX = request.form['center_x']
        centerY = request.form['center_y']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], str(id(True)) + ".jpg"))
            result = {'id' : id(), 'error' : True, 'result' : 5.5}
            return jsonify(result)
        else:
            return {'id' : -1, 'error' : False}

class Find_Transform(Resource):
    def post(self, first_id):
        result = {'id' : 1, 'error' : True, 'result' : 1.2}
        return jsonify(result)

class Find_Transform_Result(Resource):
    def get(self, first_id, second_id):
        print("Hello")
        result = {'id' : 0, 'error' : True, 'result' : 1.1}
        return jsonify(result)


api.add_resource(Image,'/image')
api.add_resource(Find_Transform,'/image/<first_id>/find-transform')
api.add_resource(Find_Transform_Result, '/image/<first_id>/find-transform/<second_id>')

if __name__ == '__main__':
    app.run(host='0.0.0.0',port='5002')

如您所见,某些终结点仍然只是测试REST api的存根。

我的Java Respond.class在这里

public class Respond {

    private int id;
    private Boolean error;
    private float result;

    public int getMessage() {
        return id;
    }


    public void setMessage(int id) {
        this.id = id;
    }

    public Boolean getError() {
        return error;
    }

    public void setError(Boolean error) {
        this.error = error;
    }

    public void setResult(float result) {
        System.out.println("Got Result");
        this.result = result;
    }

    public float getResult() {
        return result;
    }
}

我的改造界面在这里

public interface FileApi {

    @Multipart
    @POST("/image")
    Call<Respond> uploadCropImage(@Part MultipartBody.Part file, @Part MultipartBody.Part x, @Part MultipartBody.Part y);

    @Multipart
    @POST("/image/{first_id}/find-transform")
    Call<Respond> uploadCompareImage(@Path("first_id") int id, @Part MultipartBody.Part file);

    @GET("/image/{first-id}/find-transform/{second-id}")
    Call<Respond> getCompareResult(@Path("first-id") int firstId, @Path("second-id") int secondId);
}

使用两个单独的文件上传功能的原因是,第一个图像需要与用户选择的中心点一起发送,而第二个只是图像。

最后,我建立请求的部分在这里:

 private void uploadFirstImage(byte[] imageBytes) {

        FileApi service = RetroClient.getApiService();


        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageBytes);

        MultipartBody.Part fileBody =
                MultipartBody.Part.createFormData("uploaded_file", "first_image.jpg", requestFile);


        RequestBody image_center_x = RequestBody.create(MediaType.parse("multipart/form-data"), Float.toString(points[0].x));
        MultipartBody.Part xBody = MultipartBody.Part.createFormData("center_x", null, image_center_x);

        RequestBody image_center_y = RequestBody.create(MediaType.parse("multipart/form-data"), Float.toString(points[0].y));
        MultipartBody.Part yBody = MultipartBody.Part.createFormData("center_y", null, image_center_y);


        Call<Respond> resultCall = service.uploadCropImage(fileBody, xBody, yBody);

        resultCall.enqueue(new Callback<Respond>() {
            @Override
            public void onResponse(Call<Respond> call, Response<Respond> response) {

                // Response Success or Fail
                if (response.isSuccessful()) {
                    if (response.body().getError()==true){
                        System.out.println(Double.toString(response.body().getResult()));
                        session_id = response.body().getMessage();
                    }


                } else {
                    Toast.makeText(getApplicationContext(),"Failed to communicate with server",Toast.LENGTH_LONG).show();
                }


            }

            @Override
            public void onFailure(Call<Respond> call, Throwable t) {
            }
        });
    }

在这里:

private void uploadSecondImage(byte[] imageBytes) {

        FileApi service = RetroClient.getApiService();


        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageBytes);

        MultipartBody.Part fileBody =
                MultipartBody.Part.createFormData("uploaded_file", "first_image.jpg", requestFile);


        Call<Respond> resultCall = service.uploadCompareImage(session_id,fileBody);

        resultCall.enqueue(new Callback<Respond>() {
            @Override
            public void onResponse(Call<Respond> call, Response<Respond> response) {

                // Response Success or Fail
                if (response.isSuccessful()) {
                    if (response.body().getError()==true){
                        System.out.println("Got Second Response");
                        resultID = response.body().getMessage();
                    }


                } else {
                    Toast.makeText(getApplicationContext(),"Failed to communicate with server",Toast.LENGTH_LONG).show();
                }


            }

            @Override
            public void onFailure(Call<Respond> call, Throwable t) {
                System.out.println("Failure");
            }
        });
    }

最后,对于GET请求

    final boolean[] resultReady = {false};
    while (!resultReady[0]) {

        FileApi service = RetroClient.getApiService();
        Call<Respond> resultCall = service.getCompareResult(session_id, resultID);
        resultCall.enqueue(new Callback<Respond>() {
            @Override
            public void onResponse(Call<Respond> call, Response<Respond> response) {

                // Response Success or Fail
                if (response.isSuccessful()) {
                    if (response.body().getError()){
                        System.out.println("Got Response");
                        resultReady[0] = true;
                        theta[0] = response.body().getResult();
                        Toast.makeText(getApplicationContext(),"Measurement Recieved",Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(getApplicationContext(),"Error Recieved",Toast.LENGTH_LONG).show();
                    }


                } else {
                    Toast.makeText(getApplicationContext(),"Failed to communicate with server",Toast.LENGTH_LONG).show();
                }


            }

            @Override
            public void onFailure(Call<Respond> call, Throwable t) {
            }
        });



    }

我试图在OnResponse函数中放置断点,并在OnResponse和OnFailure中都放置了调试语句,并且似乎该代码从未运行过。这里可能出什么问题了?

0 个答案:

没有答案