android studio 使用post方法

    /**
     * 将图片转换成Base64编码的字符串
     */
    public String bitmaptoString(Bitmap bitmap) {

        // 将Bitmap转换成字符串

        String string = null;

        ByteArrayOutputStream bStream = new ByteArrayOutputStream();

        bitmap.compress(CompressFormat.PNG, 100, bStream);

        byte[] bytes = bStream.toByteArray();

        string = Base64.encodeToString(bytes, Base64.DEFAULT);

        return string;

    }

    /**
     * init engine
     * call in onCreate
     *
     * @return
     */
    private String onRunModel() {

        try {
            String assetImagePath="images/1.jpg";
            InputStream imageStream = getAssets().open(assetImagePath);
            Bitmap image = BitmapFactory.decodeStream(imageStream);
            String params=bitmaptoString(image);

            Log.d("params", "params_base64: "+params);

            String strUrlPath = "http://192.168.2.44:8080/TestServer/" ;

            URL url=new URL(strUrlPath);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
//            httpURLConnection.setConnectTimeout(3000);     //设置连接超时时间
            httpURLConnection.setDoInput(true);                  //打开输入流,以便从服务器获取数据
            httpURLConnection.setDoOutput(true);                 //打开输出流,以便向服务器提交数据
            httpURLConnection.setRequestMethod("POST");     //设置以Post方式提交数据
            httpURLConnection.setUseCaches(false);               //使用Post方式不能使用缓存
            //设置请求体的类型是文本类型
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            httpURLConnection.connect(); //连接即往服务端发送消息
            DataOutputStream dop=new DataOutputStream(httpURLConnection.getOutputStream());
            dop.writeBytes("data="+URLEncoder.encode(params,"utf-8"));
            dop.flush();//清空缓存
            dop.close();//关闭

            //下面开始做接收工作
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            String result="";
            String readLine=null;
            while((readLine=bufferedReader.readLine())!=null){
                result+=readLine;
            }
            bufferedReader.close();
            httpURLConnection.disconnect();
            String strResult = URLDecoder.decode(result,"utf-8");
            Log.d("strResult", "strResult: "+strResult);

            try {
                JSONObject json = new JSONObject(result);

                JSONArray ReceiptLines=json.getJSONArray("ReceiptLines");
                Log.d("ReceiptLines", "ReceiptLines: "+ReceiptLines);
                return String.valueOf(ReceiptLines);

            } catch (JSONException e) {
                e.printStackTrace();
                return "";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }


    private void onRunModelSuccessed(String result) {


        Log.i(TAG, "onRunModelSuccessed");
        textView.setText(result); //输出结果
        imageView.setImageBitmap(predictor.outputImage);
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Android Studio使用 POST 方法发送数据,你可以使用以下步骤: 1. 在你的 Android 项目中打开一个 Java 文件。 2. 导入以下依赖项: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; ``` 3. 创建一个方法来发送 POST 请求: ```java public static String sendPost(String url, String data) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 添加请求头 con.setRequestMethod("POST"); // 发送 POST 请求必须设置如下两行 con.setDoOutput(true); con.setDoInput(true); // 设置请求参数 OutputStream os = con.getOutputStream(); os.write(data.getBytes("UTF-8")); os.flush(); os.close(); // 读取响应 BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } ``` 4. 在你的代码中,调用这个方法并传递 URL 和请求参数: ```java String url = "http://example.com/api"; String data = "param1=value1&param2=value2"; String response = sendPost(url, data); ``` 这样就可以使用 POST 方法发送数据了。注意,这个示例代码中的请求参数是按照 URL 编码格式传递的。如果你使用的是 JSON 格式,可以将数据转换为 JSON 字符串再传递。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值