Eclipse 上的okhttp简单运用

1.下载jar包(okio包+okhttp包)

https://repo1.maven.org/maven2/com/squareup/okio/okio/1.13.0/okio-1.13.0.jar
https://github.com/square/okhttp
在这里插入图片描述

2.把jar包导入eclipse

(参考链接:https://jingyan.baidu.com/article/ca41422fc76c4a1eae99ed9f.html)

  1. 新建“libs”文件夹
  2. 把下载好的两个jar包复制到该文件夹
    在这里插入图片描述
  3. 选中jar包右击选“build path–>“configure build path…”–>选中”Libraries“–>add jars…–>选中jar包,之后常规操作。(两个jar包都要按照这个步骤走)
    在这里插入图片描述

新建一个Main类+工具类GetExample

http://square.github.io/okhttp/)

public class Main {
	
	  public static void main(String[] args) throws IOException {
		    GetExample example = new GetExample();
		    String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
		    System.out.println(response);
		  }
}

import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GetExample {
	OkHttpClient client = new OkHttpClient();

	public String run(String url) throws IOException {
		Request request = new Request.Builder().url(url).build();

		try (Response response = client.newCall(request).execute()) {
			return response.body().string();
		}
	}

	public GetExample() {
		// TODO Auto-generated constructor stub
	}

}

运行结果:
在这里插入图片描述

遇到的问题

  1. “java.lang.NoClassDefFoundError: okio/BufferedSource”
    (参考链接:https://www.jianshu.com/p/e5a10fdb740c)
    没有导入 okio包
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值