ipfs-shipyard / gomobile-ipfs

IPFS and libp2p on Mobile, with Gomobile
Other
315 stars 65 forks source link

[Android] How to use "add file" #141

Closed zhangchengtest closed 1 year ago

zhangchengtest commented 1 year ago

Asking a question, not reporting a bug

Is there an existing issue for this?

Question

How to use "add file"

Context

ipfs.newRequest("add").withArgument(path).send(); it is not correct, is there any way to process the "add" function

jefft0 commented 1 year ago

Hello @zhangchengtest . This command doesn't read the file from the file system. It is read from the body of the request. Have you tried one of the "body" methods? https://github.com/ipfs-shipyard/gomobile-ipfs/blob/2af807822a4b70cb3992b0141a3ca5906c3c2766/android/bridge/src/main/java/ipfs/gomobile/android/RequestBuilder.java#L234-L288

zhangchengtest commented 1 year ago

Hi, I use the method withBody like this

File file = new File(Environment.getExternalStorageDirectory(), fileName); InputStream inputStream = ipfs.newRequest("add").withBody(file).send();

But I got anthor error

go.Universe$proxyerror: read unix @->//data/user/0/com.cunw.peer/cache/sock/0000000: use of closed network connection

jefft0 commented 1 year ago

What is your test setup? Are you using an Android simulator, or connected to a real phone? Before trying new commands, do the test commands work? For example, in the packages directory:

make test_bridge.android
zhangchengtest commented 1 year ago

I use a real phone and there is another error when I use another file

go.Universe$proxyerror: Post "http://unix/api/v0/add?": EOF

jefft0 commented 1 year ago

We need to know if basic IPFS is working. Does make test_bridge.android work?

zhangchengtest commented 1 year ago

not try this, but i will try later. but I have tried a lot command like ipfs get , ipfs swam peers... they all works in the app installed in my phone only the "add" not work

zhangchengtest commented 1 year ago

make test_bridge.android have no problem

zhangchengtest commented 1 year ago

follow the example it is ok. but I got the cid not same as the cid where I add the same file to computer. ` ByteArrayOutputStream outputStream = new ByteArrayOutputStream( ); outputStream.write("--------------------------5f505897199c8c52\r\n".getBytes()); outputStream.write("Content-Disposition: form-data; name=\"file\"\r\n".getBytes()); outputStream.write("Content-Type: application/octet-stream\r\n\r\n".getBytes()); outputStream.write(buffer.toByteArray()); outputStream.write("\r\n\r\n--------------------------5f505897199c8c52--".getBytes());

byte body[] = outputStream.toByteArray();

ArrayList jsonList = ipfs.newRequest("add") .withHeader("Content-Type", "multipart/form-data; boundary=------------------------5f505897199c8c52") .withBody(body) .sendToJSONList();

String cid = jsonList.get(0).getString("Hash"); Log.d(TAG, "cid is " + cid);

zhangchengtest commented 1 year ago

witbody bytes function works, but inputstream not work always throw the EOF exception in InputStreamToGo

jefft0 commented 1 year ago

Hello @zhangchengtest . Thank you for testing. I have reproduced your error and I created a bug report. We will look into it. https://github.com/ipfs-shipyard/gomobile-ipfs/issues/143

jefft0 commented 1 year ago

Hello @zhangchengtest . We merged pull request #146 to fix the EOF exception in InputStreamToGo . This test of the API now works.

Does this resolve your issue?

jefft0 commented 1 year ago

Bug was fixed. This issue is inactive. Presumed resolved. Closing.