美文网首页
HttpUrlConnection及OkHttp使用备忘

HttpUrlConnection及OkHttp使用备忘

作者: JohnYuCN | 来源:发表于2020-04-20 14:21 被阅读0次
    package cn.johnyu.myapplication;
    
    import org.jetbrains.annotations.NotNull;
    import org.json.JSONObject;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    
    public class ExampleUnitTest {
        String url="http://47.95.10.109:8002/albums";
        
        @Test
        public void testPostWithOkHttp() throws Exception{
            //数据准备
            JSONObject obj1=new JSONObject();
            obj1.put("albumName","tomli");
            obj1.put("price",45);
            obj1.put("count",3);
    
    
            OkHttpClient client = new OkHttpClient();
    
            MediaType mediaType = MediaType
                    .parse("application/json; charset=utf-8");
    
    
            RequestBody body=RequestBody.create(obj1.toString(),mediaType);
    
            Request request = new Request.Builder()
                    .post(body)
                    .url(url)
                    .build();
    
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
    
                }
    
                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String s=response.body().string();
                    System.out.println(s);
                }
            });
    
            Thread.sleep(3000);
    
        }
    
        @Test
        public void testPost() throws Exception{
            //数据准备
            JSONObject obj1=new JSONObject();
            obj1.put("albumName","johnyu");
            obj1.put("price",45);
            obj1.put("count",3);
    
    
            //post请求
            HttpURLConnection connection=(HttpURLConnection) new URL(url).openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type","application/json");
            OutputStream outputStream=connection.getOutputStream();
    
            outputStream.write(obj1.toString().getBytes());
            outputStream.flush();
            //请求在这里发送
            connection.connect();
            InputStream inputStream= connection.getInputStream();
            int len=connection.getContentLength();
            byte[] buf=new byte[len];
            inputStream.read(buf);
            inputStream.close();
            connection.disconnect();
    
    //      测试抓取回来的数据
            String info=new String(buf);
            System.out.println(info);
            JSONObject obj=new JSONObject(info);
            String name=(String)obj.get("albumName");
            System.out.println(name);
    
        }
    }
    

    相关文章

      网友评论

          本文标题:HttpUrlConnection及OkHttp使用备忘

          本文链接:https://www.haomeiwen.com/subject/pgeibhtx.html