3

I want to use Jersey Client in my Android application. This is the code for my client:

private JSONArray getJsonElements(){
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());
    JSONArray jarray = new JSONArray();
     jarray =  service.path("/collection").accept(MediaType.APPLICATION_JSON).get(JSONArray.class);
     Log.v(TAG, jarray.toString());
    return jarray;
}
private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:6577/Example/rest/Example")
            .build();
}

And here comes the problem.When i want to build application it gives me this very common exception:

java.lang.IllegalArgumentException: already added: Ljavax/ws/rs/core/GenericEntity;...2012-07-07 16:48:32 - SM] Conversion to Dalvik format failed with error 1

I saw all questions that were asked about this exception.It is possible to remove Jars from BuildPath and change choice of client(or I can create my client),but I don't want to do this. What can you recommend to me?

Foriger
  • 459
  • 5
  • 30
  • 1
    Looks like jar conflict. Maybe you have two jars with the same packege in your libs directory. See this [question](http://stackoverflow.com/q/2680827/265597) and two most upvoted answers. – Leszek Jul 08 '12 at 10:23
  • It was this problem.Some of Jersey jars have this Class and there is a coflict.So i remove one of them and everything is work.Sorry for late answer. – Foriger Jul 17 '12 at 12:48

1 Answers1

2

Take a look in this link : http://www.vogella.com/articles/AndroidNetworking/article.html

I myself prefer to use HTTP Client to connect the Android application to a Rest Service using Jersey since it supports the HTTP commands such as POST, PUT, DELETE, GET. For example to use GET command and trasferring data in JSON format:

public class Client {

    private String server;

    public Client(String server) {
        this.server = server;
    }

    private String getBase() {
        return server;
    }

    public String getBaseURI(String str) {
        String result = "";
        try {
            HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 3000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            int timeoutSocket = 5000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
            DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpGet getRequest = new HttpGet(getBase() + str);
            getRequest.addHeader("accept", "application/json");
            HttpResponse response = httpClient.execute(getRequest);
            result = getResult(response).toString();
            httpClient.getConnectionManager().shutdown();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } 
        return result;
    }



 private StringBuilder getResult(HttpResponse response) throws IllegalStateException, IOException {
            StringBuilder result = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())), 1024);
            String output;
            while ((output = br.readLine()) != null) 
                result.append(output);

            return result;      
      }
}

And then easily in another class you can:

Client client = new Client("http://localhost:6577/Example/rest/");
String str = client.getBaseURI("Example");    // Json format
Ali
  • 9,800
  • 19
  • 72
  • 152
  • Sorry for late answer.I think that this will be beter option, instead of using Jersey.Also there is a other option- To use Android version of Restlet where everything is working ok.Thanks for help.Cheers. – Foriger Jul 17 '12 at 12:50
  • in your solution how could I pass parameters please? – William Kinaan Jun 04 '13 at 06:27
  • This is just a sample to start. Which parameter you are talking about exactly? – Ali Jun 04 '13 at 07:36