Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
luciferldy committed Dec 27, 2014
2 parents 564358f + f480811 commit 4e3019a
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 158 deletions.
1 change: 0 additions & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
# Project target.
target=android-19
android.library.reference.1=..\\..\\Eclipse\\sdk\\extras\\android\\support\\v7\\appcompat
android.library.reference.2=../open_source-libray/Android-DirectionalViewPager/Android-DirectionalViewPager-library
5 changes: 2 additions & 3 deletions res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_swipetorefresh"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:soundEffectsEnabled="true">
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/main_sv"
android:layout_width="fill_parent"
Expand All @@ -17,7 +16,7 @@

<com.directionalviewpager.DirectionalViewPager
android:id="@+id/hotstoriespagers"
android:layout_height="fill_parent"
android:layout_height="200dp"
android:layout_width="fill_parent"
android:orientation="vertical" />
<com.example.zhihupocket.ListViewForScrollViewInMainXML
Expand Down
11 changes: 8 additions & 3 deletions src/com/example/adapter/HotStoriesPagersAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.fragment.MainHotStoriesFragment;

import android.support.v4.app.Fragment;
Expand All @@ -9,9 +12,11 @@
public class HotStoriesPagersAdapter extends FragmentPagerAdapter {

private final String[] TITLES = { "one", "two", "three", "four", "five"};

public HotStoriesPagersAdapter(FragmentManager fm) {
private ArrayList<HashMap<String, Object>> top_stories = new ArrayList<HashMap<String, Object>>();

public HotStoriesPagersAdapter(FragmentManager fm, ArrayList<HashMap<String, Object>> top_stories) {
super(fm);
this.top_stories = top_stories;
}

@Override
Expand All @@ -26,7 +31,7 @@ public int getCount() {

@Override
public Fragment getItem(int position) {
return MainHotStoriesFragment.newInstance(position);
return MainHotStoriesFragment.newInstance(position, top_stories);
}

}
23 changes: 17 additions & 6 deletions src/com/example/fragment/MainHotStoriesFragment.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.zhihupocket.R;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,15 +21,21 @@ public class MainHotStoriesFragment extends Fragment {
private static final String ARG_POSITION = "position";

private int position;
private ArrayList<HashMap<String, Object>> top_stories = new ArrayList<HashMap<String,Object>>();

public static MainHotStoriesFragment newInstance(int position) {
MainHotStoriesFragment f = new MainHotStoriesFragment();
public static MainHotStoriesFragment newInstance(int position, ArrayList<HashMap<String, Object>> top_stories) {
MainHotStoriesFragment f = new MainHotStoriesFragment(top_stories);
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}


public MainHotStoriesFragment(ArrayList<HashMap<String, Object>> top_stories) {
// TODO Auto-generated constructor stub
this.top_stories = top_stories;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -43,7 +51,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
FrameLayout fl = (FrameLayout)getResources().getLayout(R.id.rl_contain_topstories);
ImageView pic = (ImageView)fl.getChildAt(0);
TextView txt = (TextView)fl.getChildAt(1);

if (top_stories.get(position).containsKey("imguri")) {
pic.setImageURI((Uri)top_stories.get(position).get("imguri"));
}
txt.setText(top_stories.get(position).get("title").toString());
return fl;
}

Expand Down
89 changes: 89 additions & 0 deletions src/com/example/task/HandleStringAndImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.example.task;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import android.net.Uri;

public class HandleStringAndImage {

public static String getHandledURL(String ori_url){
String step_one = ori_url.replace("\\", "");
String step_two = step_one.replace("[", "");
String step_three = step_two.replace("]", "");
String step_four = step_three.replace("\"", "");
return step_four;
}

// 从网络上下载图片
public static Uri downloadPic(String path, File cache){
String name = getPicNameOfUrl(path);
File file = new File(cache, name);
if(file.exists()){
//这个方法能够获得文件的URI
return Uri.fromFile(file);
}else {
//从网络上获取图片
try{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
// 返回一个URI对象
return Uri.fromFile(file);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}

// 获得图片的名称
public static String getPicNameOfUrl(String name){
String[] item_group = name.split("/");
return item_group[item_group.length-1];
}

// 清除没有图片的缓存
public static void clearImgCache(ArrayList<HashMap<String, Object>> stories_group, ArrayList<HashMap<String, Object>> topstories_group,
File pic_cache){
File[] files = pic_cache.listFiles();
int flag=0;
String img_uri;
ArrayList<String> imgs = new ArrayList<String>();

for(int i=0;i<stories_group.size();i++){
if(stories_group.get(i).containsKey("imguri")){
img_uri = stories_group.get(i).get("imguri").toString();
imgs.add(getPicNameOfUrl(img_uri));
}
}

for(File file: files){
flag=0;
if (imgs.contains(file.getName())) {
flag=1;
}
if(flag==0){
file.delete();
}
}
}
}
60 changes: 2 additions & 58 deletions src/com/example/task/ParseJsonHotStories.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package com.example.task;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.net.Uri;

import com.example.zhihupocket.MainActivity;

public class ParseJsonHotStories {
Expand Down Expand Up @@ -44,65 +37,16 @@ public void transJsonTopStoriesIntoArrayList(String json_data){
story_item.put("share_url", MainActivity.ZHIHU_STORY_API+json_topstories.getJSONObject(i).getString("id"));
}
String str = json_topstories.getJSONObject(i).getString("image");
str = getHandledURL(str);
str = HandleStringAndImage.getHandledURL(str);

story_item.put("image", str);
// 同时异步获取图片的uri
story_item.put("imguri", downloadPic(str, MainActivity.pic_cache));
story_item.put("imguri", HandleStringAndImage.downloadPic(str, MainActivity.pic_cache));
topstories_group.add(story_item);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

// 从网络上下载图片
public Uri downloadPic(String path, File cache){
String name = getPicNameOfUrl(path);
File file = new File(cache, name);
if(file.exists()){
//这个方法能够获得文件的URI
return Uri.fromFile(file);
}else {
//从网络上获取图片
try{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
// 返回一个URI对象
return Uri.fromFile(file);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}

public String getHandledURL(String ori_url){
String step_one = ori_url.replace("\\", "");
String step_two = step_one.replace("[", "");
String step_three = step_two.replace("]", "");
String step_four = step_three.replace("\"", "");
return step_four;
}

// 获得图片的名称
public String getPicNameOfUrl(String name){
String[] item_group = name.split("/");
return item_group[item_group.length-1];
}
}
60 changes: 2 additions & 58 deletions src/com/example/task/ParseJsonStories.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package com.example.task;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.net.Uri;

import com.example.zhihupocket.MainActivity;

public class ParseJsonStories {
Expand Down Expand Up @@ -48,11 +41,11 @@ public void transJsonStoriesIntoArrayList(String json_data){
// 判断数组中是否存在images这个项目
if(json_stories.getJSONObject(i).has("images")){
String str = json_stories.getJSONObject(i).getString("images");
str = getHandledURL(str);
str = HandleStringAndImage.getHandledURL(str);
// System.out.println(str);

story_item.put("images", str);
story_item.put("imguri", downloadPic(str, MainActivity.pic_cache));
story_item.put("imguri", HandleStringAndImage.downloadPic(str, MainActivity.pic_cache));
}
stories_group.add(story_item);
}
Expand All @@ -61,53 +54,4 @@ public void transJsonStoriesIntoArrayList(String json_data){
e.printStackTrace();
}
}

public String getHandledURL(String ori_url){
String step_one = ori_url.replace("\\", "");
String step_two = step_one.replace("[", "");
String step_three = step_two.replace("]", "");
String step_four = step_three.replace("\"", "");
return step_four;
}

// 从网络上下载图片
public Uri downloadPic(String path, File cache){
String name = getPicNameOfUrl(path);
File file = new File(cache, name);
if(file.exists()){
//这个方法能够获得文件的URI
return Uri.fromFile(file);
}else {
//从网络上获取图片
try{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();
// 返回一个URI对象
return Uri.fromFile(file);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}

// 获得图片的名称
public String getPicNameOfUrl(String name){
String[] item_group = name.split("/");
return item_group[item_group.length-1];
}
}
Loading

0 comments on commit 4e3019a

Please sign in to comment.