How can I get an Android app to communicate with a web server over the Internet?

I have an idea for an application, and I'm currently studying Android development. I am pretty familiar with creating simple standalone applications.

I am also familiar with PHP and hosting.

What I want to do is make an Android application to send the image to the server via the Internet and make the server return the processed image. I have no idea how to do this.

Could you tell me how I can achieve this or what topics I should study? Also, what scripts can I use to process on a web server? In particular, is it possible to use PHP or Java?

Thanks!

+4
source share
3 answers
For Image Uploading ///Method Communicate with webservice an return Yes if Image uploaded else NO String executeMultipartPost(Bitmap bm,String image_name) { String resp = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bm.compress(CompressFormat.JPEG, 75, bos); byte[] data = bos.toByteArray(); HttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("domain.com/upload_image.php"); ByteArrayBody bab = new ByteArrayBody(data, image_name); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); reqEntity.addPart("uploaded", bab); reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf")); postRequest.setEntity(reqEntity); HttpResponse response = httpClient.execute(postRequest); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String sResponse; StringBuilder s = new StringBuilder(); while ((sResponse = reader.readLine()) != null) { s = s.append(sResponse); } resp=s.toString(); } catch (Exception e) { // handle exception here Log.e(e.getClass().getName(), e.getMessage()); } return resp; } //PHP Code <?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "yes"; } else { echo "no"; } ?> 
+6
source

Usually we do this with an http connection, you can transfer the image to post params, for more information see the link

+1
source

You need to create a simple php web service that takes a parameter as image bytes and processes the image and stores it on the server. For this Android application, they will send the image data in bytes to the server using HttpPost.

To retrieve the target, you need to create another web service that will display the image file name, from where the Android application can get the image

0
source

Source: https://habr.com/ru/post/1399033/


All Articles