How to use the Android Apache HttpDelete class with parameter - java

I need to send an ID to the server and have the server to delete one record in a DB.
I want to use the HttpDelete Apache Android SDK integrated class but I cannot figure out how to use it and how to pass parameters to the server.
With the POST request I use .setEntity method on the HttpPost class.
But in HttpDelete there's no .setEntity method.
What I have so far achieved is:
HttpClient httpclient = new DefaultHttpClient();
HttpDelete httpdelete = new HttpDelete(url);
httpdelete.setHeader(HTTP.CONTENT_TYPE, "text/xml");
response = httpclient.execute(httpdelete);

HTTP DELETE requests do not have a body. You pass parameters right on the URL:
String url = "http://foo.com/bar?bing=bang"
HttpDelete httpdelete = new HttpDelete(url);

Related

HTTPClient's PATCH method not allowing body entity?

I am using Apache HTTPClient version 4.3.5.
I am trying to create a HTTP PATCH request.
HttpPatch request = new HttpPatch(ServerURL);
StringEntity params = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
request.setEntity(params);
client.execute(request);
On checking the actual request received, it didn't have a body associated with it.
Similar code is working fine for HTTP POST requests.

how to sending multipart/form-data Post Request in with use of Apache HttpComponents in java

i am creating a desktop application which send file to an tomcat server. the servlet receiver and saves file fine.
I need some help to do a java program that post in a https site. I dont know how to put the parameters because it a multpart form data contect type.. Please help! when I do a post with firefox its like this...
This will depend. I've used the following technique to upload a multi-part file to a server before, based on providing a series of form key/name pairs.
This will be depended on you own requirements and what the servlet is actually expecting...
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String name = file.getName();
entity.addPart(new FormBodyPart("someFormParameter", new StringBody("someFormName")));
/*...*/
entity.addPart("formFileNameParameter", new FileBody(file, mimeType));
HttpClient client = /*...*/
HttpPost post = new HttpPost(url.toURI());
post.setEntity(entity);
HttpResponse response = client.execute(post);
// Process response

Android Rest Client

I found so many samples for requesting a REST API, but all together are confusing, can some one please explain me a way to use http requests.
My Requirement is, I want to get data from a REST API by providing username, pwd and a key.
What I have Used was,
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
But the response is always null and these working fine when tested with browser tool by posting the same data.Is some thing wrong with my approach? please suggest me the correct way. Thank you
(1) Google I/O video session for developing REST clients
(2) search in android developer blog
(3) https://github.com/darko1002001/android-rest-client
Please try after that post your question,
I can share code snippet from my rest client developed based on (1) & (2)
Do not use Cloud to Device Messaging, instead use the latest cloud approach with android application development.
There is new library called Volley, which looks better than AsyncTask. It should be useful in developing RESTful clients.
You probably forgot to add the internet permission to the manifest file.
Add the following line.
<uses-permission android:name="android.permission.INTERNET" />
I thing you should try this,
HttpContext localContext = new BasicHttpContext();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post,localContext);
Hope this will help.
By any chance, is the server expecting a GET request for this operation? If so, you may want to use HttpGet instead of HttpPost.

How to consume RESTful web-service?

In this
tutorial written how to create REST service and how to consume it. I confused by consuming example. There we need to have on client side jersey.jar and write like this:
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
Why client need to know how web-service implemented(jersey or may be ohter implementation)? Why client side don't consume it by using simple InputStream?
In this particular tutorial you are using the jersey CLIENT to interact with a RESTful Service.
You could also just interact with the service directly by just manually creating an HTTP request and receiving the response and parsing accordingly(http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html).
The Jersey client is ultimately is just an abstraction of this to make it easier to work with.
String URL ="http://localhost:8080/MyWServices/REST/WebService/";
String ws_method_name = "getManagerListByRoleID";
String WS_METHOD_PARAMS = "";
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(URL + ws_method_name + WS_METHOD_PARAMS);
String text = null;
try {
HttpResponse httpResponse = httpClient
.execute(httpGet, httpContext);
HttpEntity entity = httpResponse.getEntity();
text = getASCIIContentFromEntity(entity);
}catch(Exception e){
e.printStackTrace();
}
Simplest way to consume Restful web services is using Spring RestTemplate.
http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate.html

How to send text by HttpPost method?

I have no idea, how to send some text using HTTPCLIENT (java // apache) library. I need to send parameters by text to server.
Any idea?
Assume you have some-remote-server as your remote server address and some-servlet as your remote servlet which accepts param1, param2 etc.. with its respective values on request. If the remote servlet accept GET call you can use below to send the request;
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(); //You could use PostMethod if servlet accept POST
String request ="http://some-remote-server/some-servlet?param1=value1&param2=value2";
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
getMethod.setURI(new URI(request, false, null));
...
And then recieve the response return from the remote servlet like this;
ObjectInputStream ois = new ObjectInputStream(getMethod.getResponseBodyAsStream());
ois.readObject();
If you can change the tool, try RestClient Tool for eclipse.
It has great support for testing restful web-services. It has option to specify,
Header Parameter,
Query Parameter,
Body Text
Request type (GET,POST,PUT,DELETE,HEAD,OPTIONS,TRACE)

Categories