Android http client upload not working in some server - android

I m using the following code to upload file to a server. It works quite well in localhost and bluehost. But it is not working in some hosting like(Go Daddy). Would anybody explain why???
data = IOUtils.toByteArray(inputStream);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(upload_url);
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), fileName);
CustomMultiPartEntity multipartEntity = new CustomMultiPartEntity();
multipartEntity.setUploadProgressListener(this);
multipartEntity.addPart("pdf", inputStreamBody);
multipartEntity.addPart("title", new StringBody(title));
multipartEntity.addPart("subject", new StringBody(subject));
multipartEntity.addPart("college", new StringBody(college));
multipartEntity.addPart("stream", new StringBody(stream));
multipartEntity.addPart("branch", new StringBody(branch));
multipartEntity.addPart("userID", new StringBody(String.valueOf(userid)));
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
I performed a lot of testing. When I retrieve the values via $_POST[] in the server, the value is null. HttpUrlConnection works in all server. But we have to write everything ourselves. I mean why to write so much code from scratch for a simple function like upload... Is there no library with simple callbacks.?

HttpClient, HttpPost.. are deprecated in API 22, So you should try to use HttpURLConnection instead.

Related

NTLM authentication in Android app

I'm using JCIFS library found here to use NTLM authentication in my android app.The app worked fine when it just went to a site and parsed an xml, but now that I added the NTLM auth it doesn't seem to be working. Can anyone tell from this snippet of code if where the problem is between the httpclient and the inputstream?
DefaultHttpClient client = new DefaultHttpClient();
client.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
client.getCredentialsProvider().setCredentials(new AuthScope("http://www.musowls.org",80),
new NTCredentials(username, password, null, "musschool"));
HttpGet request = new HttpGet("http://www.musowls.org/assignments/assignmentsbystudentxml.aspx");
HttpResponse resp = client.execute(request);
HttpEntity entity = resp.getEntity();
InputStream inputStream = entity.getContent();
Try below code it may be help you.
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
NTCredentials creds = new NTCredentials("user_name", "password", "", "http://www.musowls.org/");
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 5000);
HttpPost httppost = new HttpPost("http://www.musowls.org/assignments/assignmentsbystudentxml.aspx");
httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpResponse response = httpclient.execute(httppost); // ERROR HAPPENS HERE
responseXML = EntityUtils.toString(response.getEntity());
Log.d("Responce", responseXML);
1) Download JCIFS from here: http://jcifs.samba.org/
2) Follow the instructions here: http://hc.apache.org/httpcomponents-client-ga/ntlm.html
Been stuck on this problem for way too long.
Check out the answer in this thread to use OkHttp3 for NTLM Authenticated calls:
https://stackoverflow.com/a/42114591/3708094

how to upload file using http put in android?

I have a REST web service and android. Now I want to request Http Put using android to call web service. In my REST web service if user want to do Http Put he can request in terminal like this :
curl -H "Content-Type:application/vnd.org.snia.cdmi.dataobject" -v -T /home/student1/a.jpg http://localhost:8080/user1/folder/a.jpg
My question is how to set -T /home/student1/a.jpg in android using HttpPut?
Here is some snippet you can use:
File f = new File(...);
...
...
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut("http://mydomain.com/some/action");
MultipartEntity entity = new MultipartEntity();
entity.addPart("myFile", new FileBody(f));
httpPut.setEntity(entity);
HttpResponse response = httpclient.execute(httpPut);

How to upload .txt file to http server?

I want to upload a .txt file(only) from a device to my server. How can I do this?
I also want to be able to download another .txt file from the server to a device.
Any ideas where to start?
Thanks..
Use HttpClient and HttpPost from the HttpComponents library available for java to post a file to your server via http. You can use the MultipartEntity and/or FileEntity class for representing your file data.
see example here, or see multipart example below:
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url);
// add file content and metadata
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(targetFile, "image/jpeg");
mpEntity.addPart("userfile", cbFile);
mpEntity.addPart( "commentText", new StringBody(commentText, "text/plain",
Charset.forName( "UTF-8" )));
httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);

sotimeout not working in a multipart http post on android 2.1

I am using the apache httpclient that ships with the android sdk to upload a file on server using multipart http post. The problem is that when i turn off the wifi connection on my device and the device has no internet access and event after setting the sotimeout and connectiontimeout the code hangs on the httpclient.execute() statement indefinitely and it happens every single time.
my code is:
HttpClient httpclient = new DefaultHttpClient();
HttpConnectionParams.setSoTimeout(httpclient.getParams(), 5000);
ConnManagerParams.setTimeout( httpclient.getParams(), 5000 );
HttpConnectionParams.setSocketBufferSize(httpclient.getParams(), 8192);
HttpPost("http://myurl");
File file = new File(fileAbsolutePath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("uploadedfile", cbFile);
httppost.setEntity(mpEntity);
if(!backupCancel)
{
System.out.println("<<<<<<<<<<<<<<<<<<<<<<Actually transferring file>>>>>>>>>>>>>>>");
HttpResponse response = httpclient.execute(httppost);
}
Try this:
Assuming u have a httpClient object which is a instance of AndroidHttpClient or DefaultHttpClient
HttpParams httpParams = httpClient.getParams();
httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Timeout in milli seconds);
httpParams you get is a reference object so it executing setIntParameter will fix the issue
Unless you real have a special requirement for custom timeouts prefer using AndroidHttpClient it is very useful and solves most of our problems :) good luck
I think you should use
HttpParams lHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(lHttpParams, 3000);
HttpConnectionParams.setSoTimeout(lHttpParams, 3000);
Best Regards,~Anup

Android - Upload Large image to server

I am trying to have my application upload images to the webserver using the code below. It works sometimes, but also seems to fail with a memory error. Can someone please post an example of how to accomplish this if the file size is to big? Also, I am building this to support 1.5 and up. I don't mind if the code given to me resizes the image smaller before upload.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlString);
File file = new File(nameOfFile);
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamEntity reqEntity = new InputStreamEntity(fileInputStream, file.length());
httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
responseEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
You have two option to make your code workable.
You should use multi part approach to upload larger size file. Which i am using in my code.
Its Apache code. (Yes, you can easily port it in your Android project).
You can minimize the image resolution. by using SampleSize flag,
Follow this link.
I hope it help.
You can try following code for uploading images.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
MultipartEntity multiPart = new MultipartEntity();
multiPart.addPart("my_picture", new FileBody(new File(IMG_URL)));
httpPost.setEntity(multiPart);
HttpResponse res = httpClient.execute(httpPost);
you should really look into setChunkedStreamingMode of the connection. or, indeed, use MultipartEntity (it's apache httpcomponents library - you will easily find lots of solutions on this.
as for resizing images, it's quite simple:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; // look at the link Rajnikant gave for more details on this
Bitmap bitmap = BitmapFactory.decodeFile(filename, options);
// here you save your bitmap to whatever you want
but it is memory consuming too...

Categories

Resources