安卓前端+web后端 通信示例 POST (人脸识别)

/*
* 调用POST query/analyze方法示例
* 1.将图片转化为Base64
* 2.调用post方法进行操作
* 3.返回alert
* 作者:seven
* 时间:2017-11-30
* */
private void testQueryAnalyze() {

    //要传递的数据
    String urlStr = "http://10.201.102.125:8080/v4/query/analyze";

    InputStream strem = getResources().openRawResource(R.drawable.test);

    Bitmap bitmap = BitmapFactory.decodeStream(strem);

    String jsonStr = "{ \"analyzeOptions\": { \"attributeTypes\": { \"age\": false, \"blurriness\": false, \"eyeStatus\": false, \"gender\": false, \"minority\": false, \"mouthStatus\": false, \"pose\": false, \"quality\": false }, \"extractFeature\": true, \"extractLandmark\": true }, \"photoData\": \"" ;

    jsonStr = jsonStr + Base64.encodeToString(Bitmap2Bytes(bitmap),Base64.NO_WRAP)+"\"}" ;
    try {

        JSONObject json_test = new JSONObject(jsonStr);

        URL url = new URL(urlStr);
        //获得连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

        conn.setUseCaches(false);
        conn.setConnectTimeout(50000);
        conn.setReadTimeout(50000);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.connect();

        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(jsonStr);
        out.close();
        StringBuilder sb = new StringBuilder();

        int HttpResult = conn.getResponseCode();
        if (HttpResult == 200) {

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();
            Log.e("new Test", "" + sb.toString());
            String msg = sb.toString();
            showDialog(msg);
        } else {
            Log.e(" ", "" + conn.getResponseMessage());
            showDialog(conn.getResponseMessage());
        }


    } catch (Exception e) {
        showDialog(e.getMessage());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值