我如何可以与一个Web服务器在互联网上一个Android应用程序通信? [英] How can I make an Android app communicate with a web server over the internet?

查看:135
本文介绍了我如何可以与一个Web服务器在互联网上一个Android应用程序通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想法,一个应用程序,我目前学习Android开发。我相当熟悉创建简单的独立应用程序。

我也熟悉PHP和网站主办。

我想要做的是,让一个Android应用程序通过互联网发送图像到服务器,使服务器返回一个处理后的图像。我不知道我会怎么做。

能否请你告诉我,我该怎么去实现这个或者我应该考虑哪些主题?另外,我可以用什么脚本来完成处理Web服务器上?特别是,我可以使用PHP和Java?

谢谢!

解决方案

 有关图片上传
///沟通的方法与web服务的回报是,如果图片上传旁若无人
字符串executeMultipartPost(位图BM,弦乐IMAGE_NAME){
    字符串RESP = NULL;
    尝试 {
        ByteArrayOutputStream BOS =新ByteArrayOutputStream();
        bm.com preSS(比较pressFormat.JPEG,75,BOS);
        byte []的数据= bos.toByteArray();
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost postRequest =新HttpPost(domain.com/upload_image.php);
        ByteArrayBody BAB =新ByteArrayBody(数据,IMAGE_NAME);

        MultipartEntity reqEntity =新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart(上传,BAB);
        reqEntity.addPart(photoCaption,新StringBody(sfsdfsdf));
        postRequest.setEntity(reqEntity);
        HTT presponse响应= httpClient.execute(postRequest);
        的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),UTF-8));
        字符串sResponse;
        StringBuilder的S =新的StringBuilder();
        而((sResponse = reader.readLine())!= NULL){
            S = s.append(sResponse);
        }
        RESP = s.toString();
    }赶上(例外五){
        //这里处理异常
        Log.e(e.getClass()的getName(),e.​​getMessage());
    }
    返回RESP;
}

// PHP code
< PHP

    $目标=上传/;

    $目标= $的目标。基本名($ _ FILES ['上传'] ['名称']);
    $确定= 1;
    如果(move_uploaded_file($ _ FILES ['上传'] ['tmp_name的值'],$目标))
    {
        显示yes;
    }
    其他 {
        回声无;
    }
?>
 

I have an idea for an app and am currently learning Android development. I'm fairly familiar with creating simple standalone apps.

I'm also familiar with PHP and webhosting.

What I want to do is, make an android app send an image to a server via the internet and make the server return a processed image. I have no clue how I'd do that.

Can you please tell me how can I go about achieving this or which topics should I look into? Also, what scripts can I use to do the processing on the web server? Particularly, can I use PHP or Java?

Thanks!

解决方案

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";
    }
?> 

这篇关于我如何可以与一个Web服务器在互联网上一个Android应用程序通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆