马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这一节比较复杂,我们还是新建个项目,修改build.gradle(app),在dependencies标签下添加implementation 'com.squareup.okhttp3:okhttp:3.4.1'
我们新更新的android studio 3.0跟以前添加依赖库的方法不一样了,以前是compile现在换成了implementation了。然后在AndroidManifest.xml里manifest标签下添加<uses-permission android:name="android.permission.INTERNET"/>
在mainactivity.java旁新建HttpCallbackListener.javapackage com.example.xinwei.myhttpconnection;
/**
* Created by xinwei on 2017/11/6.
*/
public interface HttpCallbackListener {
void onFinish(String response);
void onError(Exception e);
}
再接着新建HttpUtil.javapackage com.example.xinwei.myhttpconnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/**
* Created by xinwei on 2017/11/6.
*/
public class HttpUtil {
public static void sendOkHttpRequest(String address,okhttp3.Callback callback){
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder()
.url(address)
.build();
client.newCall(request).enqueue(callback);
}
public static void sendHttpRequest(final String address,final HttpCallbackListener listener){
new Thread(){
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder response=new StringBuilder();
String line;
while ((line=reader.readLine())!=null){
response.append(line);
}
if (listener!=null){
listener.onFinish(response.toString());
}
} catch (Exception e) {
if (listener!=null){
listener.onError(e);
}
} finally {
if (connection!=null){
connection.disconnect();
}
}
}
}.start();
}
}
修改mainactivity.javapackage com.example.xinwei.myhttpconnection;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String address="http://www.fishC.com";
//方法一
HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
public static final String TAG ="HttpUtil.sendHttpRequest" ;
@Override
public void onFinish(String response) {
Log.d(TAG, "onFinish: "+response);
}
@Override
public void onError(Exception e) {
e.printStackTrace();
}
});
//方法二
HttpUtil.sendOkHttpRequest("http://www.baidu.com", new okhttp3.Callback() {
private static final String TAG = "HttpUtil.sendOkHttpRequest";
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseData = response.body().string();
Log.d(TAG, "onResponse: "+responseData);
}
});
}
}
打开模拟器运行这个项目,就会在控制台显示网上的数据了,我们新更新的3.0有个新功能就是点击上边的按钮不用重新编译整个项目就可以显示修改后的模样,如图:
|