Issue
I am trying to connect to an Infura node from Java Android application. I was following these documents to connect to an infura node.
https://kauri.io/managing-storage-in-a-java-application-with-ipfs/3e8494f4f56f48c4bb77f1f925c6d926/a
https://github.com/ipfs-shipyard/java-ipfs-http-client/issues/115
Code:
package com.example.javahttp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import io.ipfs.api.IPFS;
public class MainActivity extends AppCompatActivity {
IPFS ipfs ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTask().execute();
}
private class MyTask extends AsyncTask<Void , Void, Void > {
@Override
protected Void doInBackground(Void... voids) {
IPFS ipfs = new IPFS("/dnsaddr/ipfs.infura.io/tcp/5001/https");
try{
System.out.println("connected");
System.out.println("id: "+ ipfs.id());
}catch(Exception e){
System.out.println("not connected"+e);
}
return null;
}
}
}
I am getting this error.
java.lang.RuntimeException: IOException contacting IPFS daemon.
Trailer: null ipfs method not allowed
Any suggestions please.
Solution
I don't get why there is such an error but when I used a plain java class to connect with it. It was possible ,you may refer here to my repository blockchain with java to see if you have all the relevant dependencies and you have been doing it properly because I am not familiar with android but I am familiar with web3j.
You can use the below code to connect with an infura node and parse a file to it.
import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class App {
public static void main(String[] args) throws IOException {
IPFS ipfs = new IPFS("/dnsaddr/ipfs.infura.io/tcp/5001/https");
try {
NamedStreamable.InputStreamWrapper is = new NamedStreamable.InputStreamWrapper(new FileInputStream());
MerkleNode response = ipfs.add(is).get(0);
} catch (IOException ex) {
throw new RuntimeException("Error whilst communicating with the IPFS node", ex);
}
}
}
Answered By - fuzious
Answer Checked By - Katrina (JavaFixing Volunteer)