Currently I am connecting from android to a .net WEB API using HttpClient and I have been able to do a GET and POST to read/write data. However I want to do an Update and a Delete.
I tried to do this using a POST, but it simple creates more records. Here is my code for the POST, how would I change it to do a PUT or DELETE instead?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.net/api/employees/6");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("firstName", "UpdatedHello"));
nameValuePairs.add(new BasicNameValuePair("lastName", "World"));
nameValuePairs.add(new BasicNameValuePair("employee_name", "UpdatedHello World"));
nameValuePairs.add(new BasicNameValuePair("password", "xxx"));
nameValuePairs.add(new BasicNameValuePair("isActive", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
yeah! the document of httpClient http://hc.apache.org/httpclient-3.x/methods.html
You have PutMethod and DeleteMethod API for performing PUT and DELETE Http requests. The sample usage as follows as per doc
PUT Request - The put method is very simple, it takes a URL to put to and requires that the body of the request method be set to the data to upload. The body can be set with an input stream or a string.This method is generally disabled on publicly available servers because it is generally undesireable to allow clients to put new files on the server or to replace existing files.
PutMethod put = new PutMethod("http://jakarta.apache.org");
put.setRequestBody(new FileInputStream("UploadMe.gif"));
// execute the method and handle any error responses.
...
// Handle the response. Note that a successful response may not be
// 200, but may also be 201 Created, 204 No Content or any of the other
// 2xx range responses.
DELETE Request - The delete method is used by supplying a URL to delete the resource at and reading the response from the server.This method is also generally disabled on publicly available servers because it is generally undesireable to allow clients to delete files on the server.
DeleteMethod delete = new DeleteMethod("http://jakarata.apache.org");
// execute the method and handle any error responses.
...
// Ensure that if there is a response body it is read, then release the
// connection.
...
delete.releaseConnection();
Related
I am trying to connect to apache tomcat server using HTTP POST, when i see LOG file of server it showing GET /login/validate_doc.jsp HTTP/1.1" 200 685 ,
which means it is getting a GET request when i am sending using HttpPost and form parameters are not received by server.
my code is below:
HttpPost post_http=null;
post_http=new HttpPost("http://somexx.ac.in/medONmob/validate_doc.jsp");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
post_http.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Where am i wrong ...??? Help me out please
Try specifying encoding when constructing UrlEncodedFormEntity. By default it is ISO-8859-1.
Also this will make your code future safe
Creating a UrlEncodedFormEntity from a List of NameValuePairs throws a NullPointerException
Given that you are using a post, then you probably are sending data on your request body, Am I right?, then you have to specify the content-type of the data you are sending in the headers, in order to execute a proper post:). For example if I am sending a json in the request body then I should add a header like this:
request.addHeader("content-type", "text/json");
Cheers
I want to send data from my app to http://studentssp.wit.ie/Timetables/POSTT.aspx that page and I'm not sure how to do that. All that I have tried atm is adding some of the form values to the url to see if that changes anything, but it hasn't. e.g. I put cboSchool=EP (the name value of the select is cboSchool and the value of the School of Adult Education is EP) into the url to see if it would only load information regarding that school. http://studentssp.wit.ie/Timetables/POSTT.aspx?cboSchool=EP
I'm knew to android and know nothing about aspx.
This should get your started:
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name1", "value1"));
nameValuePairs.add(new BasicNameValuePair("name2", "value2"));
nameValuePairs.add(new BasicNameValuePair("name3", "value3"));
// etc...
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
I think that should work for what you're trying to do. I have TIMEOUT_MS set to 10000 (so, 10 seconds)
Then you can read out the server's response using something like this:
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent
1- you talk about mobile application based on web , so if you need to interact with server side , you need web service .
please check the web service you need to integrate with site you need .
2- most of web service will be (Restful format ,SOAP).
Restful is easier , you can parse data with any open source parse SAX , DOM for XML format ,
or Json Parser for json .
i will give you simple example :
this is a currency converter API , and we will sent the parameters in URL this is mean http Get request ,
Like :
http://www.google.com/ig/calculator?hl=en&q=100GBP=?USD
there is "?hl=en&q=1GBP=?USD "
any parameter have 2 parts is 1-key 2- value .
h1 is key , en is value .
q is key , 1GEP=?USD is value .
and the respone will be in json format Like
{lhs: "100 British pounds",rhs: "161.47 U.S. dollars",error: "",icc: true}
you can see this is link for parsing json .
i hope this help you?
Is it possible to set 2 entities for a HttpPost? Like:
HttpPost post = new HttpPost("http://www.abc.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("A",
a));
nameValuePairs.add(new BasicNameValuePair("B", b));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
post.setHeader("Accept-Encoding", "gzip");
ByteArrayEntity bae = new ByteArrayEntity(compress(json));
post.setEntity(bae);
HttpResponse resp;
resp = client.execute(post);
I'm trying to achieve telling the server that there are some parameters and a zip file.
yes You can send zip file and pass parameter using nameValuePairs. go to below link you may get your solution.
http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/
Android upload multi files to server via http post
in this link place your zip file address on place of image. and you may have to do some more modification.
Not like this. You need use a multi-part entity, you can manually encode it if it is relatively simple, or use org.apache.http.entity.mime.MultipartEntity (which is not part of the Android SDK). There are multiple post about it on SO, search for 'android multipart'.
I want to create an Application, the description of it as follows
Perform search
Display the results.
Basically, I want to create an application for an existing web-site which does this, but my need is to create for a Mobile.
Based on the website link, it refers to some ASP pages. for e.g. http://test.com/query.asp. The website allows the user to enter various input for search.
I want to create and then send the same request to the server and get the result. How can I know what all parameters/headers it is taking and what format is the response (XML or JSON).
Below is my sample code for the Android Device
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List nameValuePairs = new ArrayList(3);
//this is where you add your data to the post method
nameValuePairs.add(new BasicNameValuePair("sel", "1"));
nameValuePairs.add(new BasicNameValuePair("txt", "2466"));
nameValuePairs.add(new BasicNameValuePair("selr", "2011"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httpPost);
content = response.getEntity().getContent();
return content;
// Update
Ok, now I am able to get the desired output from the web query.
I printed the result for my Http Post request and I saw some HTML tags in it. In short, it prints the source of the HTML page that has to be displayed.
Is there a way to display the HTML view in my Android Application? Or a way to fetch only the relavant data from the response and ignore the HTML tags etc..?
First line of the response looks like
06-17 16:35:21.756: DEBUG/(30307): <script language="javascript" type=text/css>
//
Regards,
Nirav
You need to speak with the owner/developer of the web site / service that you want to access. Only they can reliably tell you what their APIs are if you are unable to determine them yourself from WireShark captures.
I'm trying to write a java code on an Android emulator that will send a string to a web service writen in c#.
Android code:
HttpPost httppost = new HttpPost("http://192.168.2.1:53811/WinnerSite/WebService.asm/MyMethod
try {
// Add your data
List nameValuePairs = new ArrayList(2);
nameValuePairs.add(new BasicNameValuePair("json", name));
// nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Also tried:
HttpPost httppost = new HttpPost("http://192.168.2.1:53811/WinnerSite/WebService.asm/MyMethod
The web-service is on the same machine as the emulator. MyMethod is accessable through:
http://localhost:53811/WinnerSite/WebService.asmx/MyMethod
Does someine ahs an idea?
The code exits on the "httpclient.execute(httppost);" line
The eclipse shows:
"ActivityThread.prefo
Source not found."
I have already solve a persmission problem (adding a note to the emolator's xml)
Thanks,
When you want to use the network, you should add network access permission in your AndroidManifest.xml.
Your problem seems to be complex. Check your client app and web service separately to assure they are both correct.
Your codes posted seems to be correct. But your error message "ActivityThread.prefo Source not found." is too weak... Please provide more info.