Send large post param in Android - android

How do you send a large (>5MB) post parameter (among other post parameters) in Android?
I'm currently using org.apache.http.client.methods.HttpPost and a list of BasicNameValuePair to send the post params.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(someURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
nameValuePairs.add(new BasicNameValuePair("param2", hugeValue));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
hugeValue is a very large string that I would like to read from an InputStream and send as I read it. How can I do this?

CommonsWare is right. HttpUrlConnection and writing direct into the outputstream will solve your memory problem. I've used it in my own application for uploading 10 and more mb of image data.
A good example according your kind of post request is: http://www.java.happycodings.com/Other/code21.html

Related

Can I use HttpClient to do a PUT or Delete

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();

HttpPost in android not working

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

Android HttpPost with Gzip and NameValuePair

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'.

Trying to send a string from an Android emulator to a webservice

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.

Android: Get response from a https url

Greetings,
I'm developing an Android app and need to open a url (with POST parameters) over https and get the response.
There's the added complication that I have a self-signed certificate. I also need to accept cookies.
Anyone have any ideas about where to get started?
Many thanks in advance,
Android comes with the apache commons http library included.
Setting up a https post request is quite easy:
HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value"));
nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
Android uses a version 4.x of the commons http library as all versions below 4.0 are out of their lifecycle.
I can't tell exactly how to register a self-signed certificate to the HttpClient, but mybe the commons http documentation helps:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
I managed to get it all working asyncronously with both cookies and unsigned https.
I used the code here:
http://masl.cis.gvsu.edu/2010/04/05/android-code-sample-asynchronous-http-connections/
and modified for unsigned https using Brian Yarger's code here:
Self-signed SSL acceptance on Android
(Add the above code to the beginning of run() in HttpConnection.java)
To get the cookies to work, I had to modify some code (POST snippet from HttpConnection.java):
case POST:
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(data));
httpPost.addHeader("Cookie", Cookie.getCookie());
response = httpClient.execute(httpPost);
Header[] headers=response.getAllHeaders();
for(int i=0;i<headers.length;i++){
if(headers[i].getName().equalsIgnoreCase("Set-Cookie")){
//Log.i("i",headers[i].getName()+"---"+headers[i].getValue());
Cookie.setCookie(headers[i].getValue());
break;
}
}
break;
Many thanks to everyone for pointing me in the direction,

Categories

Resources