简体   繁体   中英

okhttp string problem if using string in .url

I'm using API from a website and it needs two strings String one: modelId String two: inferenceId both of them are equal to value


OkHttpClient client = new OkHttpClient();

                Request request = new Request.Builder()
                        .url("https://api.leapml.dev/api/v1/images/models/"+modelId+"/inferences/"+inferenceId)
                        .get()
                        .addHeader("accept", "application/json")
                        .addHeader("authorization", "Bearer *secret word here*")
                        .build();
                
                Response response = client.newCall(request).execute();


My issue is in Example: A

.uri("https://api.leapml.dev/api/v1/images/models/"+modelId+"/inferences/"+inferenceId)

if I added both modelId string and inferenceId string the the above code, the API doesn't work correctly

but if I add the text directly there like in below, everything works great Example: B

.url("https://api.leapml.dev/api/v1/images/models/1285ded4-b11b-4993-a491-d87cdfe6310c/inferences/3f9f5c8d-320f-4afc-85c4-454522118c16")

Both of the above gives me the EXACT same result if I got the value of request.url(); but the API gets faulty with the example A But works fine with example B

Log of Example A (faulty one)

I/System.out: Second Step: {"id":"f9828985-af41-46be-b8bc-ef81504b4a87","state":"queued","prompt":"cow","seed":4523184,"width":1024,"height":1024,"numberOfImages":1,"steps":50,"weightsId":"8b1b897c-d66d-45a6-b8d7-8e32421d02cf","workspaceId":"d76af992-7c3e-4b1a-bc72-46b27de7c377","createdAt":"2023-02-01T09:56:42.255006+00:00","images":[],"modelId":"8b1b897c-d66d-45a6-b8d7-8e32421d02cf"}

Log with Example B (Working perfect)

I/System.out: Second Step: {"id":"3f9f5c8d-320f-4afc-85c4-454522118c16","state":"finished","prompt":"cow","seed":4523184,"width":512,"height":512,"numberOfImages":1,"steps":50,"weightsId":"1285ded4-b11b-4993-a491-d87cdfe6310c","workspaceId":"d76af992-7c3e-4b1a-bc72-46b27de7c377","createdAt":"2023-02-01T08:54:31.244159+00:00","images":[{"id":"f6518aea-f2a3-47ac-b9ff-81ca8c996206","uri":"https://dreamtrain.s3.us-west-2.amazonaws.com/image-gen-3f9f5c8d-320f-4afc-85c4-454522118c16/generated_images/0.png","createdAt":"2023-02-01 08:54:42.784652+00"}],"modelId":"1285ded4-b11b-4993-a491-d87cdfe6310c"}

As you can see Example B has "uri" image link and that's what i need.

I tried many things but it seemed like nothing help.

String url = "https://api.leapml.dev/api/v1/images/models/"+modelId+"/inferences/"+inferenceId;

System.out.println(url); // This should be equal to "https://api.leapml.dev/api/v1/images/models/1285ded4-b11b-4993-a491-d87cdfe6310c/inferences/3f9f5c8d-320f-4afc-85c4-454522118c16"

// Use the variable as argument
.url(url);

If this still not work. Please provide the logs.

It seems your are trying to add binary data to the string. Check whether your are getting the actual string which you need.

I found the issue and the solution, The API I'm using (Leapml) takes about 10 seconds to generate me the image link. So if I let the thread sleep for 11 seconds everything works just great

Thanks for everyone tried to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM