【声明】转载请注明出处,此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail
——尊重作者,知识无价,交流无限!
在Android中获取网络数据是非常重要的,也是非常耗时的一个操作。笔者知道一般是用两种方式来获取网络数据:
1、开启一条子线程来实现。
2、采取异步加载的方式。
好了,不废话了,直接上代码!
1、Activity
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.ListView;
public class ShumaActivity extends Activity {
private static final String TAG = "MainActivity";
private ListView listView;
private BookAdapter adapter;
private GridView gv_shuma;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// listView=(ListView) findViewById(R.id.listView);
gv_shuma=(GridView) findViewById(R.id.gv_shuma);
new NetTask().execute();
}
class NetTask extends AsyncTask<Void, Void, List<Book>>{
@Override
protected List<Book> doInBackground(Void... params) {
List<Book> list =new ArrayList<Book>();
//网络操作
HttpClient client =new DefaultHttpClient();
HttpGet get =new HttpGet("http://192.168.93.1:8080/myCloud/book/json");
try {
HttpResponse response=client.execute(get);
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
//OK
HttpEntity entity=response.getEntity();
String data=EntityUtils.toString(entity, "utf-8");
Log.d(TAG, data);
//数据解析
//创建json数组,将数据放进去
JSONArray array=new JSONArray(data);
for(int i=0;i<array.length();i++){
//将普通的对象强制转换成Json对象
JSONObject o= (JSONObject) array.get(i);
String isbn=o.getString("isbn");
String title=o.getString("title");
String author=o.getString("author");
String image=o.getString("image");
Book book=new Book(isbn, title, author, image);
list.add(book);
}
}else{
Log.d(TAG, "失败:"+response.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//异步执行完后操作,还有一个异步执行前的方法没重写
@Override
protected void onPostExecute(List<Book> result) {
super.onPostExecute(result);
Log.d(TAG, result.toString());
adapter=new BookAdapter(ShumaActivity.this, result);
<pre name="code" class="java" style="color: rgb(54, 46, 43); font-size: 14px; line-height: 18px; "><pre name="code" class="java" style="color: rgb(54, 46, 43); font-size: 14.44444465637207px; line-height: 17.981481552124023px; "><span style="white-space:pre"> </span>gv_shuma<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 14px; line-height: 18px; ">.setAdapter(adapter);</span>
}}}
2、自定义适配器BookAdapter
import java.util.List;
import com.squareup.picasso.Picasso;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class BookAdapter extends BaseAdapter{
private List<Book> data;
private LayoutInflater inflater;
private Context context;
public BookAdapter(Context context, List<Book> data){
this.context=context;
this.data=data;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
convertView=inflater.inflate(R.layout.book_item,parent, false);
holder=new ViewHolder();
holder.image=(ImageView) convertView.findViewById(R.id.iv_shuma_item);
holder.isbn=(TextView) convertView.findViewById(R.id.isbn_shuma_item);
holder.title=(TextView) convertView.findViewById(R.id.title_shuma_item);
holder.author=(TextView) convertView.findViewById(R.id.author_shuma_item);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
Book b = data.get(position);
holder.isbn.setText(b.getIsbn());
holder.title.setText(b.getTitle());
holder.author.setText(b.getAuthor());
String urlImg="http://192.168.93.1:8080/myCloud"+b.getImage();
Log.d("TAG",urlImg);
//引用一个Picasso的库,加载图片
Picasso.with(context).load(urlImg).into(holder.image);
return convertView;
}
static class ViewHolder{
ImageView image;
TextView isbn;
TextView title;
TextView author;
}
}
3、Book的Bean文件
public class Book {
private String isbn;
private String title;
private String author;
private String image;
public Book(){
}
public Book(String isbn, String title, String author, String image) {
super();
this.isbn = isbn;
this.title = title;
this.author = author;
this.image = image;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Book [isbn=" + isbn + ", title=" + title + ", author=" + author
+ ", image=" + image + "]";
}
}
好人做到底,以下是布局文件
4、/解析Json数据/res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ShumaActivity" >
<GridView
android:id="@+id/gv_shuma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"/>
</RelativeLayout>
5、/解析Json数据/res/layout/book_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="@+id/iv_shuma_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/title_shuma_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="华为荣耀6pp"/>
<TextView
android:id="@+id/author_shuma_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="华为荣耀6pp"/>
<TextView
android:id="@+id/isbn_shuma_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="华为荣耀6pp"/>
</LinearLayout>
我懂你,哈哈☞源码地址:http://download.csdn.net/detail/huntersnail/8809095
☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆转载请注明出处☞指尖飞落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆