Add Library
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.google.code.gson:gson:2.6.2'
OkhttpListViewActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bhaumik.networkingtask.OKhttpListViewActivity">
<ListView
android:id="@+id/okhttp_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
okhttp_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="@android:color/holo_blue_light">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textColor="@android:color/background_light"
android:textSize="20dp"
android:maxLength="5"
android:text="Hello World"/>
</android.support.v7.widget.CardView>
OkhttpListViewActivity.java
package com.example.bhaumik.networkingtask;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.bhaumik.networkingtask.adapter.OkhttpListAdapter;
import com.example.bhaumik.networkingtask.pojo.Image;
import com.example.bhaumik.networkingtask.pojo.Network;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OKhttpListViewActivity extends AppCompatActivity {
OkhttpListAdapter adapter;
List<Image> list = new ArrayList<>();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_okhttp_list_view);
listView = (ListView) findViewById(R.id.okhttp_listview);
adapter = new OkhttpListAdapter(list,OKhttpListViewActivity.this);
listView.setAdapter(adapter);
new MyTask().execute();
}
public class MyTask extends AsyncTask<Void,Void,String>{
String url = "http://bing.com/HPImageArchive.aspx?format=js&idx=0&n=20&mkt=en-US";
@Override
protected String doInBackground(Void... voids) {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
Gson gson = new Gson();
Network network = gson.fromJson(response.body().string(),Network.class);
List<Image> list_data = network.getImages();
for (Image image : list_data){
list.add(image);
}
return "Size -> " + list.size();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
}
}
OkhttpListAdapter.java
package com.example.bhaumik.networkingtask.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.bhaumik.networkingtask.R;
import com.example.bhaumik.networkingtask.pojo.Image;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bhaumik on 9/3/2017.
*/
public class OkhttpListAdapter extends BaseAdapter{
List<Image> list = new ArrayList<>();
Context context;
public OkhttpListAdapter(List<Image> list, Context context) {
this.list = list;
this.context = context;
}
@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 position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
View view = LayoutInflater.from(context).inflate(R.layout.okhttp_item_list,parent,false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
Image image = list.get(position);
viewHolder.textView.setText(image.getCopyright());
return viewHolder.itemView;
}
public static class ViewHolder{
TextView textView;
View itemView;
public ViewHolder(View itemView) {
this.itemView = itemView;
textView = itemView.findViewById(R.id.text_view);
}
}
}
0 comments:
Post a Comment