zoukankan      html  css  js  c++  java
  • okhttp的Post方式

     

    发送post请求

    public class MainActivity extends AppCompatActivity {
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            new Thread(runnable).start();
        }
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
                String requestBody = "I am Jdqm.";
                Request request = new Request.Builder()
                        .url("https://api.github.com/markdown/raw")
                        .post(RequestBody.create(mediaType, requestBody))
                        .build();
                OkHttpClient okHttpClient = new OkHttpClient();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("输出:", "onFailure: " + e.getMessage());
                    }
    
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.d("输出:", response.protocol() + " " +response.code() + " " + response.message());
                        Headers headers = response.headers();
                        for (int i = 0; i < headers.size(); i++) {
                            Log.d("输出:", headers.name(i) + ":" + headers.value(i));
                        }
                        Log.d("输出:", "onResponse: " + response.body().string());
                    }
                });
            }
        };
        }
  • 相关阅读:
    react 学习
    redux saga学习
    Power BI连接至Amazon Redshift
    php时间日期
    layui select 禁止点击
    微信小程序二维码是无法识别二维码跳转到小程序
    JSON字符串与JSON对象的区别
    前端切图要选择png和jpg呢?
    @media媒体查询
    TortoiseGit revert failed
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/10991653.html
Copyright © 2011-2022 走看看