/* * 调用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()); } }
安卓前端+web后端 通信示例 POST (人脸识别)
最新推荐文章于 2023-03-16 18:55:19 发布