HttpURLConnection、HttpClient网络请求
2017-10-08 20:18
344 查看
package com.bwie.skn.zuoye1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bwie.skn.zuoye1.bean.JsonBean;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
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 java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<JsonBean.NewslistBean> list = new ArrayList<>();//创建集合
private ListView lv;
private MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
mAdapter = new MAdapter();
lv.setAdapter(mAdapter);
}
public void HttpURLConnection(View view){
//list.clear();
//mAdapter.notifyDataSetChanged();
//mAdapter = new MAdapter();
//lv.setAdapter(mAdapter);
new MHttpURLConnectionAsyncTask().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");
Toast.makeText(MainActivity.this,list.size()+"个",Toast.LENGTH_SHORT).show();
}
public void HttpClient(View view){
//list.clear();
//mAdapter.notifyDataSetChanged();
//mAdapter = new MAdapter();
//lv.setAdapter(mAdapter);
new MHttpClientAsyncTask().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");
Toast.makeText(MainActivity.this,list.size()+"个",Toast.LENGTH_SHORT).show();
}
//适配器
class MAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHordel vh;
if(view==null){
view = View.inflate(MainActivity.this,R.layout.lv_item_layout,null);
vh = new ViewHordel();
vh.image=view.findViewById(R.id.image);
vh.title=view.findViewById(R.id.title);
view.setTag(vh);
}else{
vh = (ViewHordel) view.getTag();
}
vh.title.setText(list.get(i).getTitle());
new MAsyncTaskImage(vh.image).execute(list.get(i).getPicUrl());
return view;
}
}
class ViewHordel{
ImageView image;
TextView title;
}
//网络获取图片
class MAsyncTaskImage extends AsyncTask<String,Void,Bitmap>{
ImageView image;
public MAsyncTaskImage(ImageView image) {
this.image = image;
}
@Override
protected Bitmap doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
4000
InputStream inputStream = urlConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
image.setImageBitmap(bitmap);
}
}
//HttpURLConnection获得数据
class MHttpURLConnectionAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode==200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String string="";
while ((string=reader.readLine())!=null){
buffer.append(string);
}
return buffer.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
List<JsonBean.NewslistBean> newslist = jsonBean.getNewslist();
list.addAll(newslist);
mAdapter.notifyDataSetChanged();
}
}
//HttpClient获得数据
class MHttpClientAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
//1.创建HttpClient
HttpClient hc=new DefaultHttpClient();
//2.创建HttpGet对象
HttpGet hg = new HttpGet(strings[0]);
//3.执行请求
try {
HttpResponse response = hc.execute(hg);
//4.得到结果吗
int code = response.getStatusLine().getStatusCode();
if(code==200){
//5.得到结果数据
HttpEntity result = response.getEntity();
String string = EntityUtils.toString(result);
return string;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
List<JsonBean.NewslistBean> newslist = jsonBean.getNewslist();
list.addAll(newslist);
mAdapter.notifyDataSetChanged();
}
}
}
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bwie.skn.zuoye1.bean.JsonBean;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
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 java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<JsonBean.NewslistBean> list = new ArrayList<>();//创建集合
private ListView lv;
private MAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
mAdapter = new MAdapter();
lv.setAdapter(mAdapter);
}
public void HttpURLConnection(View view){
//list.clear();
//mAdapter.notifyDataSetChanged();
//mAdapter = new MAdapter();
//lv.setAdapter(mAdapter);
new MHttpURLConnectionAsyncTask().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");
Toast.makeText(MainActivity.this,list.size()+"个",Toast.LENGTH_SHORT).show();
}
public void HttpClient(View view){
//list.clear();
//mAdapter.notifyDataSetChanged();
//mAdapter = new MAdapter();
//lv.setAdapter(mAdapter);
new MHttpClientAsyncTask().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");
Toast.makeText(MainActivity.this,list.size()+"个",Toast.LENGTH_SHORT).show();
}
//适配器
class MAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHordel vh;
if(view==null){
view = View.inflate(MainActivity.this,R.layout.lv_item_layout,null);
vh = new ViewHordel();
vh.image=view.findViewById(R.id.image);
vh.title=view.findViewById(R.id.title);
view.setTag(vh);
}else{
vh = (ViewHordel) view.getTag();
}
vh.title.setText(list.get(i).getTitle());
new MAsyncTaskImage(vh.image).execute(list.get(i).getPicUrl());
return view;
}
}
class ViewHordel{
ImageView image;
TextView title;
}
//网络获取图片
class MAsyncTaskImage extends AsyncTask<String,Void,Bitmap>{
ImageView image;
public MAsyncTaskImage(ImageView image) {
this.image = image;
}
@Override
protected Bitmap doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode==200){
4000
InputStream inputStream = urlConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
image.setImageBitmap(bitmap);
}
}
//HttpURLConnection获得数据
class MHttpURLConnectionAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode==200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String string="";
while ((string=reader.readLine())!=null){
buffer.append(string);
}
return buffer.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
List<JsonBean.NewslistBean> newslist = jsonBean.getNewslist();
list.addAll(newslist);
mAdapter.notifyDataSetChanged();
}
}
//HttpClient获得数据
class MHttpClientAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
//1.创建HttpClient
HttpClient hc=new DefaultHttpClient();
//2.创建HttpGet对象
HttpGet hg = new HttpGet(strings[0]);
//3.执行请求
try {
HttpResponse response = hc.execute(hg);
//4.得到结果吗
int code = response.getStatusLine().getStatusCode();
if(code==200){
//5.得到结果数据
HttpEntity result = response.getEntity();
String string = EntityUtils.toString(result);
return string;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
List<JsonBean.NewslistBean> newslist = jsonBean.getNewslist();
list.addAll(newslist);
mAdapter.notifyDataSetChanged();
}
}
}
相关文章推荐
- Android 网络请求HttpURLConnection 和 HttpClient详解
- 网络请求部分-------HttpClient和HttpURLConnection
- 网络请求:HttpURLConnection、HttpClient、HttpUtils、Volley
- HttpClient和HttpURLConnection网络请求数据
- HttpClient与HttpURLConnection网络请求工具类
- 网络请求HttpURLConnection与HttpClient的使用
- Android网络框架volley学习(四)网络请求HttpURLConnection与HttpClient浅析
- Android 中的两种请求网络资源的方式:HttpClient和HttpURLConnection
- HttpURLConnection、HttpClient请求网络数据、Xutils的HttpUtils 请求数据
- 关于Android网络访问请求的一些总结 包括HttpClient HttpUrlConnection Volley
- HttpClient和HttpURLConnection网络请求数据
- 赠送 HttpClient 和HttpURLConnection 的轻型网络框架 --》线程池介绍和网络请求流程
- Android网络常用的请求---HttpURLConnection和HttpClient
- 常用的几种网络请求OkHttpClient , HttpClient和 HttpURLConnection
- Android网络连接之HttpURLConnection和HttpClient
- 网络层架构设计与实战七框架拓展设计之支持原生的HttpUrlConnection方式请求和响应
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android开发之网络请求通信专题(一):基于HttpURLConnection的请求通信
- Java网络连接之HttpURLConnection与HttpClient 区别及联系(转)
- Java网络连接之HttpURLConnection与HttpClient 区别及联系