0

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

Ratul Doley
  • 499
  • 1
  • 6
  • 12

1 Answers1

0

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

xxx
  • 3,315
  • 5
  • 21
  • 40
  • I don't want to rewrite things from scratch... I mean I have to write lot of craps in httpurlconnection. Suggest me some library if u have used one... – Ratul Doley Jun 23 '16 at 12:37
  • You have different options to manage networking in Android. Follow this article for alternatives library: http://stackoverflow.com/questions/6341400/alternative-http-client-library-for-android – xxx Jun 23 '16 at 12:46