1.webView控件:这是一个浏览器的控件,主要是实现显示网页的客户端,通过loadUrl(String url)方法可以访问指定的网站。
通过setWebViewClient(
WebViewClient view)方法可以定义一个游览器的实例,里面new 一个WebViewClient对象可以保持在当前界面打开。
设置其他参数,通过getSettings()方法设置,比如设置允许支持JavaScript脚本. getSettings().setJavaScriptEnabled(true).
记得增加权限!!!
2.Http连接方法:
创建一个Http对象,使用 URL url = new URL(String url);
HttpURLConnection connection = url.openConnection();
设置一些参数,比如设置request的方法:GET还是POST。超时时间等。
connection.setRequestMethod("GET");
connection.setConnectionTimeout(10000);
connection.setReadTimeout(10000);
读取输入的信息:
InputStream in = connection.getInputStream();
BufferReader reader = new BufferReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while (((line = response.readLine()) != null){
response.append(line);
}
这里就将URL中的内容都拼接起来存放到StringBuilder对象中,但是不是String类型,需要强制转化toString()才能使用。
然后在新建一个方法体,在方法中穿件UI线程,实现对UI的操作就可以.
runOnUiThread(new Runnable()){
public void run(){
...
}
}
记得实现关闭操作,比如reader的关闭,http的关闭,以及相关的Exception异常处理。
3.OKHttp使用:
首先,创建OKHttpClient client = new OKHttpClient()对象,
创建需要请求的对象Request request = new Request.Builder().bulid();这期间连接其他方法丰富Request对象,比如url(String url) 添加访问的地址。
之后就是获取服务器的数据,newCall()方法返回一个Call对象,调用execute()方法来发送请求并返回服务器的数据。
Response response = client.newCall(resquest).execute();
获得数据的主体
String responseData = response.body().string();
然后就是对数据的json解析。
JSON解析:
首先需要对String responseData 进行解析。
创建JSONArray 对象,JSONArray jsonArray = new JSONArray(String responseData);
进行循环解析:
for (int i = 0; i < jsonArray.length(); i ++){
创建一个JSONObject对象,
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject对象进行键值对匹配,就能解析到对应的内容。
}
4.GSON的使用:
首先,添加grandle:
compile
'com.google.code.gson:gson:2+'
创建一个GSON gson = new GSON();
List<APP> appList = gson.fromJson( responseData(要解析的数据), new TypeToken<
List<APP>>(){}.getType());
利用一个循环体foreach()结构将数据解析出来:
for (App app : appList){
app.getAppname;
app.getApp
}
需要强调的几点:
1.APP 类中的每一个字段都要与JSON中的一致,不然无法获得需要的数据内容。
2.List<APP> 是一个整体,主要不要丢掉 List<>。
不然会报错如下:
stackoverflow的解决答案为:
https://stackoverflow.com/questions/9598707/gson-throwing-expected-begin-object-but-was-begin-array#
5.HttpUtil工具类的使用:
a.HttpURLConnection 使用java的回调机制来实现:
首先创建一个回调的接口,public interface HttpCallbackListener{
构造两个方法,一个为成功的时候调用, void onFinish(String respsonse);
一个为失败的时候调用, void onError(Exception e);
}
在HttpUtil类中实现该接口的参数调用。
public static void sendHttpResquest(final String address, final HttpCallbackListener listener){
new Thread(new Runnable){
public void run(){
HttpURLConnection connection = null;
try{
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnnection();
connection.setRequestMathod("GET");
connection.setReadTimeout(10000);
connection.setConnectionTimeout(10000);
connection.setDoOutput(true);
connection.setDoInput(true);
InputStream = connection.getInputStream();
BufferReader reader = new BufferReader(new InputStreamReader(in));
StringBulider response = StringBulider();
String line;
while ((line = reader.reanLine()) != 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.disconnection();
}
}
}
}).start();
}
然后再在调用HttpUtil.sendConnectionResquest(address, new HttpCallbackListener(){
public void onFinish(String repsonse){
//这里进行JSON数据的解析操作
}
public void onError(Exception e){
//这里进行错误处理操作
}
});
b.OKHttp的回调。
首先是在HttpUtil类中,public static void sendOKHttpResquest(String address, okhttps3.Callback callback){
OKHttpClient client = new OKHttpClient();
Resquest resquest = new Resquest.Builder().url(address).build();
client.newCallback(resquest).enqueue(callback);//
主要是这句话 enqueue()方法(已经封装设置了回调)而不是
execute()方法。
}
然后再在调用HttpUtil.sendOKHttpResquest(url, new okhttp3.Callback(){
public void onResponse(Call call, Response response) throws IOException{
String responseData = response.body().string(); //转化为String类型,然后就可以解析了
}
public void onFailure (Call call, IOException e){
//这里进行异常情况的处理
}
})
核心:1.主要是对URL的传值操作处理
2.回调机制的利用