首页 > 其他分享 >App 实现的两类网络请求

App 实现的两类网络请求

时间:2022-12-14 19:38:19浏览次数:51  
标签:请求 url App 两类 Override new progressDialog httpURLConnection String


1.Java HttpUrl 的实现如下:

1.1 Get 请求

{
//显示进度
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("正在获取数据请稍后!");
progressDialog.show();

//启动分线程
new Thread(){
@Override
public void run() {
String strPath = et_network_url.getText().toString();
URL url =null;

HttpURLConnection httpURLConnection = null;
try {
url = new URL(strPath);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
httpURLConnection.connect();
int Recode = httpURLConnection.getResponseCode();
if(Recode==200){
InputStream is = httpURLConnection.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[]{};
int len =-1;
while ((len=is.read(buffer))!=-1){
byteArrayOutputStream.write(buffer,0,len);
}
final String result = byteArrayOutputStream.toString();
is.close();
byteArrayOutputStream.close();
}
httpURLConnection.connect();
runOnUiThread(new Runnable() {
@Override
public void run() {
//et_network_result.setText(result);
progressDialog.dismiss();
}
});

} catch (Exception e) {
e.printStackTrace();
}finally {
progressDialog.dismiss();
}
}
}.start();
}

1.2 Post 请求

{

final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("正在获取数据请稍后!");
progressDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
String strPath = et_network_url.getText().toString();
URL url =null;

HttpURLConnection httpURLConnection = null;
try {
url = new URL(strPath);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
httpURLConnection.connect();
OutputStream os = httpURLConnection.getOutputStream();
String data = "name=Tom&age=11";
os.write(data.getBytes("utf-8"));
//成功
if(httpURLConnection.getResponseCode()==200){
InputStream is = httpURLConnection.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[]{};
int len =-1;
while ((len=is.read(buffer))!=-1){
byteArrayOutputStream.write(buffer,0,len);
}
final String result = byteArrayOutputStream.toString();
is.close();
byteArrayOutputStream.close();
}
os.close();
httpURLConnection.connect();

runOnUiThread(new Runnable() {
@Override
public void run() {
//et_network_result.setText(result);
progressDialog.dismiss();
}
});

}catch (Exception ex){

}finally {
progressDialog.dismiss();
}

}
}).start();
}

2.Volley 框架实现:

2.1 Get 请求

//创建请求对象
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("正在获取数据请稍后!");
progressDialog.show();
String strPath = et_network_url.getText().toString();
StringRequest stringRequest = new StringRequest(strPath, new Response.Listener<String>() {
@Override
public void onResponse(String response) { //主线程执行
et_network_result.setText(response);
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void one rrorResponse(VolleyError error) {
progressDialog.dismiss();
}
});
requestQueue.add(stringRequest);

 

2.2 Post 请求

//创建请求对象
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("正在获取数据请稍后!");
progressDialog.show();
String strPath = et_network_url.getText().toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, strPath, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
et_network_result.setText(response);
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void one rrorResponse(VolleyError error) {
progressDialog.dismiss();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {

Map<String,String> map = new HashMap<String, String>();
map.put("name", "Tom6");
map.put("age", "16");
return map;
}
};
requestQueue.add(stringRequest);

 

标签:请求,url,App,两类,Override,new,progressDialog,httpURLConnection,String
From: https://blog.51cto.com/u_15461374/5938243

相关文章