前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >borrow用法及短语(that is ok用法)

borrow用法及短语(that is ok用法)

作者头像
全栈程序员站长
发布2022-07-25 21:04:11
3.2K0
发布2022-07-25 21:04:11
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

    String path="https://www.zhaoapi.cn/ad/getAd";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void getTong(View view) {
        new Thread(){
            @Override
            public void run() {
                super.run();
                OkHttpClient okHttpClient = new OkHttpClient();//创建客户端对象  Request builder = new Request.Builder()
                        .url(path)//指定访问路径  .build();//提交请求  Call call = okHttpClient.newCall(builder);//用客户端调用请求对象  //开始执行 ....指定是同步还是异步  Response execute = null;//同步  try {
                    execute = call.execute();
                    if(execute.isSuccessful()){
 
 //判断是否请求成功  final String pp=execute.body().string();
// Log.i("jiba","------"+pp);  runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,pp.substring(0,20),Toast.LENGTH_SHORT).show();
                            }
                        });

                    }else{
                        Log.i("jiba","你是sss!");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();

    }

    public void getYi(View view) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request builder = new Request.Builder().url(path).build();
        Call call = okHttpClient.newCall(builder);
        call.enqueue(new Callback() {
            //请求错误所走的方法  @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();//打印错误日志  }
            //请求成功所走的方法  @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                Log.i("jiba","======"+s);
            }
        });
    }

    public void postTong(View view) {
        OkHttpClient okHttpClient = new OkHttpClient();
        FormBody formBody = new FormBody.Builder()
                .add("mobile","18360981994")
                .add("password","123456")
                .build();
        Request builder = new Request.Builder()
                .post(formBody)
                .url("https://www.zhaoapi.cn/user/reg")
                .build();
        Call call = okHttpClient.newCall(builder);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, string,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    public void postShangchuan(View view) {
        //A.检查用户是否已经允许了权限....PackageManager.PERMISSION_GRANTED代表的是用户已经允许   if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //B.不允许...的时候,,,请求用户允许这个权限  // Activity arg0代表当前的activity, @NonNull String[] arg1请求的权限的数组,也就是需要请求允许哪些权限, int arg2请求码  ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1001);

        }else {
            //允许...上传文件  postFile();
        }


    }

    private void postFile() {
        OkHttpClient okHttpClient = new OkHttpClient();
        //2.指定文件的类型 image/jpg image/png video/mp4 ...mimeType  MediaType mediaType = MediaType.parse("text/x-markdown;charset=utf-8");
        //3.指定要上传的文件对象  File file = new File(Environment.getExternalStorageDirectory(),"wang.txt");
        Request request = new Request.Builder()
                //上传文件的时候请求体使用RequestBody.create()获取okhttp3.MediaType contentType  // 文件的类型,@NotNull java.io.File file上传的文件对象  .post(RequestBody.create(mediaType, file))
                .url("https://api.github.com/markdown/raw")
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Toast.makeText(MainActivity.this,response.body().string(),Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }
}
代码语言:javascript
复制
布局
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  android:orientation="vertical"  android:layout_height="match_parent" tools:context="one.bw.com.okhttpqingqiu.MainActivity">

   <Button  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="okhttp的get同步"  android:onClick="getTong"  />
    <Button  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="okhttp的get异步"  android:onClick="getYi"  />
    <Button  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="okhttp的post异步"  android:onClick="postTong"  />
    <Button  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="post上传文件"  android:onClick="postShangchuan"  />

</LinearLayout>

                                                        发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127786.html原文链接:https://javaforall.cn
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档