I am a rookie in android and now i encounter with such issue. I need sent a zip directory to the phyton server and the first i have tried make it with help of MultipartEntity, but it is use a HttpClient and HttpPost which don't support now with sdk 23 and depricate now.
And i figure out that i can use OkHttp instead. I found some samples, but they clear up how sent json file. There is loads of samples but no one how to sent a dir...
Who have a sample how to sent a zip directory, help me please...
Thanks!
try to use this:
HttpClient http = AndroidHttpClient.newInstance("MyApp");
HttpPost m = new HttpPost("http://url-to-your-python-server");
m.setEntity(new FileEntity(new File("path-to-zip-file"), "application/octet-stream"));
HttpResponse response = http.execute(m);
Related
While searching the solution for this issue, I read somewhere that max size of get request is 8Kb. However when I am trying to execute get request of content length of only 248 bytes and total URL length of only 282 characters through Apache HttpClient execute method, Apache HttpClient is giving me error: org.apache.http.HttpException: HTTP/1.1 413 Request Entity Too Large.
However the same get request (the same URL) gives expected response in browser (and NOT "413 Request Entity Too Large").
Apache HttpClient execute method is working fine for some other get request which is slightly smaller in length and has lesser no. of query params.
I also tried sending the Post request but still got the same error.
Please help me resolve this issue. Any help will be appreciated
The other seemingly similar questions didn't solve my problem.
Request Entity too large error, comes due to server receiving request that are larger than configured to process. It should not be from the client side, you need to modify you server setting to allow larger request body length. This parameter will differ server to server.
Some of them are listed here
Sorry the issue was not on client side. But the internal API that I am using was sending incorrect 413 response code instead of (almost) correct response code 507.
WHAT I DID:
I have tried to call the web service by "POST" method from my android app using the following code.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://myserver.com:5060/convert/service");
try {
File file = new File(inputPath);
httppost.setHeader("Content-Type", "application/msword");
httppost.setHeader("Accept", "application/pdf");
httppost.setEntity(new ByteArrayEntity(readFile(file)));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
e.printStackTrace();
}
When I do this api call, I am getting the following exception.
"org.apache.http.NoHttpResponseException: The target server failed to
respond"
But when I do the same web service call from standalone java project using the same code, I am getting the correct response.
Not getting any errors like above.
WHAT I WANT:
Based on my java web service call, I know that there is no issue in service and also the source code. So What may be the issue? How can I get rid of this issue?
I have fixed the issue.
The issue is because of port number 5060. I did found this by using TCP/IP monitor tool in eclipse.
By defaule 5060 port is not being called from android(I didn't know the reason). Because the request not hits the server.
So I have changed the port number to 8090 in my server and restarted the server.
Then I called the service from android. Now It's working great.
I need to send data from my web interface to linux server. I am using tomcat as server. I am new to java, i visited many questions but didn't find any exact solution. Other than code i would like to see help regarding process/logic to send data on linux server. Manual i post data on server like this, that i need to post by web interface throuhg HTTP.
curl --header "Content-type: application/json" --request POST --data '{"name":"John", "id":"500", "employee":"yes","salary":"5000","dept":"accounts"}' http://serverNumericURL.com
I'd recommend using a library like apache http components
Example:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://serverNumericURL.com");
httpPost.setEntity(new StringRequestEntity(jsonString, "application/json", "UTF-8"));
httpClient.executeMethod(httpPost);
Or if you would like to stick to the JDK, you could use the HTTPURLConnection as in the following link which covers both HTTP GET and POST.
http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
This question already has answers here:
How to use java.net.URLConnection to fire and handle HTTP requests
(12 answers)
Closed 5 years ago.
I need to upload images and txt files from my application to a remote server (Just http no ftp) using java. My application is in jsf framework. I searched but no suitable things found.
Can anybody guide me?
In fact I should upload files to special folder to remote server.
I have two application with shared path to upload files, so for accessing them to this files, I decidec to upload shared files(such as images and texts) to third server. First application should upload files to this remote server and second application should read them from it.
So my hard part of this solution is to upload files to this third server(in fact remote server) using http.
To upload file to a specific folder, your server API must support that.
Server side for receiving uploaded files, you can use http://commons.apache.org/fileupload/
Client side for sending a file upload request, you can use https://hc.apache.org/httpcomponents-client-ga/index.html
Have a look at apache commons-fileupload. You can find sample code here.
Use following code:
byte[] data = bos.toByteArray();//convert ur file into byte[]
HttpClient httpClient = new DefaultHttpClient();//Client
HttpPost postRequest = new HttpPost(YOUR_SERVER_URL);//Post Request to specified URL
ByteArrayBody bab = new ByteArrayBody(data, "a.txt");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);// Multipart data
reqEntity.addPart("uploadingFile", bab); //adding data to request entity
postRequest.setEntity(reqEntity);//adding request entity to post request
HttpResponse response = httpClient.execute(postRequest);
As per your requirement, you need to send multiple images and text files. So HTTP multi-part file upload seems to be a suitable solution. You can get further information on this from here.
You can use HttpClient.
Send the files using POST as a method.
make
#Autowired
ServletContext c;
or take object
byte[] bytes = file.getBytes();
String UPLOAD_FOLDEdR=c.getRealPath("/images");
Path path = Paths.get(UPLOAD_FOLDEdR +"/"+ file.getOriginalFilename());
Files.write(path, bytes);
System.out.println(path);
String Pic_Name =file.getOriginalFilename();
I have a Gingerbread Android application that I'm porting to ICS. This application communicates with a web server sending HTTP POST. My application runs fine on Gingerbread. However, I have been experiencing problems after porting it to ICS. I found out that the POST requests my application is sending are actually changed to GET.
The funny thing is, Android actually reports that POST is indeed used.
URL oURL = new URL(sURL);
HttpURLConnection oHTTPConnection = (HttpURLConnection)(oURL.openConnection());
oHTTPConnection.setDoInput(true);
oHTTPConnection.setDoOutput(true);
oHTTPConnection.setRequestMethod("POST");
// set headers...
int nResponse = oHTTPConnection.getResponseCode();
String sMethod = oHTTPConnection.getRequestMethod(); // Returns "POST"
However, the server would say otherwise. I modified the web server application to check the request method it receives and then put this value in the response body it sends back to my Android application. And what I receive on my Android application is "GET".
I have tried using HttpClient with HttpPost but I get the same issue.
As I mentioned, I didn't have this problem in Gingerbread. Also, I've read from another thread here a similar (but opposite) problem that also only happens in ICS: Android 4.0 ICS turning HttpURLConnection GET requests into POST requests.
Has anyone else experienced this? Can anyone help me solve this?
Thanks in advance!
Rai
Try follow this answer: https://stackoverflow.com/a/8799198/372076
I've found that pre-ICS one could get away with making a body-less
POST without providing a Content-Length value, however post-ICS you
must set Content-Length: 0.
Don't know if you already found a fix for this, but i was having the same problem and just found a work around. In my case it was an issue on server's side with Apache's redirection. I was doing:
Url url = new Url("http://aaaa.bbbb.com/");
Changed to:
Url url = new Url("http://aaaa.bbbb.com/index.php");
Somehow the redirection was turning my POST into a GET with no parameters.