java - Add Jersey client to my Android application.Illegal Argument Exception occured? -
i want use jersey client in android application. code 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 problem.when want build application gives me common exception:
java.lang.illegalargumentexception: added: ljavax/ws/rs/core/genericentity;...2012-07-07 16:48:32 - sm] conversion dalvik format failed error 1
i saw questions asked exception.it possible remove jars buildpath , change choice of client(or can create client),but don't want this. can recommend me?
take in link : http://www.vogella.com/articles/androidnetworking/article.html
i myself prefer use http client connect android application rest service using jersey since supports http commands such post, put, delete, get. example use command , 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 in class can:
client client = new client("http://localhost:6577/example/rest/"); string str = client.getbaseuri("example"); // json format
Comments
Post a Comment