ipfs-shipyard/gomobile-ipfs

[Android] How to use "add file"

zhangchengtest opened this issue · 12 comments

Asking a question, not reporting a bug

  • This question is not about a bug

Is there an existing issue for this?

  • I have searched the existing issues

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

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?

// Body methods
/**
* Adds an InputStream body to the request.
*
* @param body The InputStream from which to read the body
* @return This instance of RequestBuilder
* @see <a href="https://docs.ipfs.io/reference/api/http/">IPFS API Doc</a>
*/
public RequestBuilder withBody(@NonNull InputStream body) {
Objects.requireNonNull(body, "body should not be null");
requestBuilder.body(new InputStreamToGo(body));
return this;
}
/**
* Adds a string body to the request.
*
* @param body The string value of the body to add
* @return This instance of RequestBuilder
* @see <a href="https://docs.ipfs.io/reference/api/http/">IPFS API Doc</a>
*/
public RequestBuilder withBody(@NonNull String body) {
Objects.requireNonNull(body, "body should not be null");
requestBuilder.bodyString(body);
return this;
}
/**
* Adds a byte array body to the request.
*
* @param body The byte array value of the body to add
* @return This instance of RequestBuilder
* @see <a href="https://docs.ipfs.io/reference/api/http/">IPFS API Doc</a>
*/
public RequestBuilder withBody(@NonNull byte[] body) {
Objects.requireNonNull(body, "body should not be null");
requestBuilder.bodyBytes(body);
return this;
}
/**
* Adds a file as a body to the request.
*
* @param body The file to add as a body
* @return This instance of RequestBuilder
* @throws FileNotFoundException If the file is inaccessible
* @see <a href="https://docs.ipfs.io/reference/api/http/">IPFS API Doc</a>
*/
public RequestBuilder withBody(@NonNull File body) throws FileNotFoundException {
Objects.requireNonNull(body, "body should not be null");
FileInputStream fis = new FileInputStream(body);
requestBuilder.fileBody(body.getName(), new InputStreamToGo(fis));
return this;
}

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

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

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

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

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

make test_bridge.android have no problem

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);

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

Hello @zhangchengtest . Thank you for testing. I have reproduced your error and I created a bug report. We will look into it.
#143

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?

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