简单的判断当前网络存储sd卡

本教程展示如何使用Android和异步HTTP客户端实现一个可下拉刷新和加载更多的城市列表应用。应用从网络获取城市数据,并能根据层级不同调整列表项布局权重。
public class MainActivity extends AppCompatActivity {

private XListView listViewB;
private ArrayList<Info.Result> parent_list;
private ArrayList<Info.Result> result;
private int index=1;
private ArrayList<Info.Result> list_now;
private MyAdapterB myAdapterB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listViewB = (XListView) findViewById(R.id.listviewB);
getNetData();
listViewB.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(MainActivity.this,DetailActivity.class);
intent.putExtra("city",result.get(position).city);
startActivity(intent);
}
});
listViewB.setPullRefreshEnable(true);
listViewB.setPullLoadEnable(true);
listViewB.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
//刷新
listViewB.postDelayed(new Runnable() {
@Override
public void run() {
Log.i("---",index+"");
list_now.clear();
index=0;
int j=index+20;
for (int i=index;i<j;i++){
index++;
Log.i("now---",index+"---");
list_now.add(result.get(i));
}
myAdapterB.notifyDataSetChanged();//刷新适配器
listViewB.stopRefresh();

}
},2000);

}

@Override
public void onLoadMore() {
listViewB.postDelayed(new Runnable() {
@Override
public void run() {
Log.i("---",index+"");
int j=index+20;
for (int i=index;i<j;i++){
index++;
Log.i("now---",index+"---");
list_now.add(result.get(i));
}
myAdapterB.notifyDataSetChanged();//刷新适配器
listViewB.stopLoadMore();

}
},2000);

}
});


}

//网络请求数据
void getNetData() {
String url = "http://api.jisuapi.com/weather/city?appkey=b4d06fdd59ed379f";
AsyncHttpClient client = new AsyncHttpClient();
client.get(MainActivity.this, url, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//失败
Log.i("测试", "失败");
}

@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
//成功
// Log.i("测试","成功"+responseString);
String path = Environment.getExternalStorageDirectory()
.getPath() + "/json.txt";
File file = new File(path);
try {
file.createNewFile();//创建文件
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(responseString);
fileWriter.flush();//刷新
fileWriter.close();

} catch (IOException e) {
e.printStackTrace();
}
getData(responseString);
}
});
}

//解析数据 且分类
void getData(String responseString) {
Gson gson = new Gson();
Info info = gson.fromJson(responseString, Info.class);
result = info.result;
list_now = new ArrayList<Info.Result>();
for (int i=0;i<31;i++){
index++;
list_now.add(result.get(i));//得到30条数据
}
myAdapterB = new MyAdapterB();
listViewB.setAdapter(myAdapterB);

}


class MyAdapterB extends BaseAdapter {

@Override
public int getCount() {
return list_now.size();
}

@Override
public Object getItem(int position) {
return list_now.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 = convertView.inflate(MainActivity.this, R.layout.itemb, null);
holder = new ViewHolder();
holder.itemtext = (TextView) convertView.findViewById(R.id.itemtext);
holder.item_null = (TextView) convertView.findViewById(R.id.text_null);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.itemtext.setText(list_now.get(position).city);
if (list_now.get(position).parentid.equals("0")){
// 动态设置权重,宽,高;参数: 宽 高 权重
holder.itemtext.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
holder.item_null.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

}else{
holder.itemtext.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));
holder.item_null.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
}


return convertView;
}
}

class ViewHolder {
TextView itemtext;
TextView item_null;
}

}





public class DetailActivity extends Activity{

private TextView city;
private TextView weather;
private TextView tem;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_main);
city = (TextView) findViewById(R.id.city);
weather = (TextView) findViewById(R.id.weather);
tem = (TextView) findViewById(R.id.tem);
Intent intent = getIntent();
String cityname = intent.getStringExtra("city");
String path="http://api.jisuapi.com/weather/query?appkey=b4d06fdd59ed379f&city="+cityname;
AsyncHttpClient client = new AsyncHttpClient();
client.get(DetailActivity.this, path, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//失败
Log.i("测试", "失败");
}

@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
//成功
// Log.i("测试","成功"+responseString);
String path = Environment.getExternalStorageDirectory()
.getPath() + "/jsondetail.txt";
File file = new File(path);
try {
file.createNewFile();//创建文件
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(responseString);
fileWriter.flush();//刷新
fileWriter.close();

} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
InfoB infoB = gson.fromJson(responseString, InfoB.class);
//String cityname = infoB.result.city;
city.setText(infoB.result.city);
weather.setText("天气"+infoB.result.weather);
tem.setText("温度"+infoB.result.templow+"-"+infoB.result.temphigh);

}
});


}



public class InfoB {
public ResultB result;

public class ResultB {
public String city;
public String weather;
public String templow;
public String temphigh;


}


public class Info {
public ArrayList<Result> result;

public Info(ArrayList<Result> result) {
this.result = result;
}

public class Result{
public String city;
public String cityid;
public String parentid;

public Result(String city, String cityid, String parentid) {
this.city = city;
this.cityid = cityid;
this.parentid = parentid;
}
}
}


public class NetUtil {
public static boolean isNetWorkAvailable(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}

return false;
}
}









boolean available = NetUtil.isNetWorkAvailable(MainActivity.this);
if (available){
//如果有网络 那么就开始请求数据 并且吐司显示有网络
Toast.makeText(MainActivity.this, "当前网络可用", Toast.LENGTH_LONG).show();
getNetData();
}else{
//没有网络 吐司 解析缓存的json
// new File(getEx);
Toast.makeText(MainActivity.this, "没有网络", Toast.LENGTH_LONG).show();
String path = Environment.getExternalStorageDirectory()
.getPath()+"/json.txt";
File file = new File(path);
if (file.exists()){
//判断是否存在
try {
FileReader reader = new FileReader(file);
char[] c=new char[1024];

int len=0;
try {
while ((len=reader.read(c))!=-1){
s = new String(c, 0, len);
sb.append(s);
}
Log.i("测试缓存",s);
getData(sb.toString());

} catch (IOException e) {
e.printStackTrace();
}


//reader.read()


} catch (FileNotFoundException e) {
e.printStackTrace();
}
}



}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值