笔记自用 来源:阳光沙滩 拉大锯
关于http响应码
http请求格式
android权限配置
网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
关于Android 版本,网络请求的问题: 权限配置了,还是无法发起请求。这个其实可以去看log,log会告诉你为什么不给你请求。
也有同学说在版本27的时候可以,到了29上就不可以了。 遇到这些问题怎么办呢?可以去看官方文档
android28以前的版本,比如说27可以使用明文(http)访问,27以后需要网络安全配置,28(包括28)以上,则需要配置 否则就会报下面的错:
1.配置这个可以解决:
2.配置networkSecurityConfig:,使用网络安全配置文件:
允许这些域名明文访问,否则http会报错
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">sunofbeaches.com</domain>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">www.sunofbeach.net</domain>
<domain includeSubdomains="true">imgs.sunofbeaches.com</domain>
</domain-config>
</domain-config>
</network-security-config>
简单读取:
public void loadJson(View view){
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://10.0.2.2:9102/get/text");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10000); //连接时间
connection.setRequestMethod("GET");//请求方法
//设置请求头里面的一些属性
connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
connection.setRequestProperty("Accept","*/*");
connection.connect();
//结果码
int responseCode = connection.getResponseCode();
if(responseCode==200){
//拿头部信息
Map<String, List<String>> headerFields = connection.getHeaderFields();
//alt +enter 自动补全
Set<Map.Entry<String, List<String>>> entries = headerFields.entrySet();
for(Map.Entry<String,List<String>> entry:entries){
Log.d(TAG,entry.getKey()+"=="+entry.getValue());
}
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
Log.d(TAG,"line-->"+line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
展示数据 利用Gson 和Glide加载图片 小测试:
//适配器
package com.example.mytext.adapters;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.mytext.R;
import com.example.mytext.domain.GetTextItem;
import java.util.ArrayList;
import java.util.List;
public class GetResultListAdpters extends RecyclerView.Adapter<GetResultListAdpters.InnerHolder> {
//防止为null
private List<GetTextItem.DataBean> mdata = new ArrayList<>();
@NonNull
@Override
public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_get_text,null);
InnerHolder innerHolder = new InnerHolder(view);
return innerHolder;
}
@Override
public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
View itemView = holder.itemView;
TextView title = itemView.findViewById(R.id.item_title);
ImageView iv = itemView.findViewById(R.id.iv_item);
GetTextItem.DataBean dataBean = mdata.get(position);
title.setText(dataBean.getTitle());
Glide.with(itemView.getContext()).load("http://10.0.2.2:9102"+mdata.get(position).getCover()).into(iv);
}
@Override
public int getItemCount() {
return mdata.size();
}
public void setData(GetTextItem getTextItem) {
mdata.clear();
mdata.addAll(getTextItem.getData());
notifyDataSetChanged();
}
public class InnerHolder extends RecyclerView.ViewHolder {
public InnerHolder(@NonNull View itemView) {
super(itemView);
}
}
}
package com.example.mytext;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.mytext.adapters.GetResultListAdpters;
import com.example.mytext.domain.GetTextItem;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TextActivity extends AppCompatActivity {
private static final String TAG ="TextActivity" ;
private GetResultListAdpters adpters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
initView();
}
private void initView(){
RecyclerView recyclerView = findViewById(R.id.result_list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
outRect.top =5;
outRect.bottom =5;
}
});
adpters =new GetResultListAdpters();
recyclerView.setAdapter(adpters);
}
public void loadJson(View view){
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://10.0.2.2:9102/get/text");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10000); //连接时间
connection.setRequestMethod("GET");//请求方法
//设置请求头里面的一些属性
connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
connection.setRequestProperty("Accept","*/*");
connection.connect();
//结果码
int responseCode = connection.getResponseCode();
if(responseCode==200){
//拿头部信息
Map<String, List<String>> headerFields = connection.getHeaderFields();
//alt +enter 自动补全
Set<Map.Entry<String, List<String>>> entries = headerFields.entrySet();
for(Map.Entry<String,List<String>> entry:entries){
Log.d(TAG,entry.getKey()+"=="+entry.getValue());
}
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
//Log.d(TAG,"line-->"+line);
Gson gson = new Gson();
GetTextItem getTextItem = gson.fromJson(line,GetTextItem.class);
updateUI(getTextItem);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private void updateUI(GetTextItem getTextItem) {
runOnUiThread(new Runnable() {
@Override
public void run() {
adpters.setData(getTextItem);
}
});
}
}