java HTTP 协议下的文件下载
1. 建立HTTP的链接,返回输入流。
2. 读取输入流,写入输出流。
import java.net.*;
import java.io.*;
class demo{
public static void main(String[] args) {
HttpUtils hu = new HttpUtils("http://bizingeinoutetyou.com/bizinkisya/wp-content/uploads/2015/04/kiritanimirei_i04.jpg");
hu.saveImageToDisk();
}
}
class HttpUtils{
// private static String url_path = "http://bizingeinoutetyou.com/bizinkisya/wp-content/uploads/2015/04/kiritanimirei_i04.jpg";
private static String url_path ;
public HttpUtils(String url_path){
this.url_path = url_path;
}
public void saveImageToDisk(){
InputStream inputstream = getInputStream();
FileOutputStream fileoutputstream = null;
byte[] buff = new byte[1024];
int len = 0;
try{
fileoutputstream = new FileOutputStream("test.jpg");
while((len = inputstream.read(buff)) != -1){
fileoutputstream.write(buff,0,len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(inputstream != null){
try{
inputstream.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(fileoutputstream != null){
try{
fileoutputstream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static InputStream getInputStream(){
InputStream inputstream = null;
HttpURLConnection httpurlconnection = null;
try{
URL url = new URL(url_path);
httpurlconnection = (HttpURLConnection)url.openConnection();
httpurlconnection.setConnectTimeout(3000);
httpurlconnection.setDoInput(true);
httpurlconnection.setRequestMethod("GET");
int responseCode = httpurlconnection.getResponseCode();
if(responseCode == 200){
inputstream = httpurlconnection.getInputStream();
}
System.out.println(url.getFile());
System.out.println(httpurlconnection.getResponseCode());
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return inputstream;
}
}
2016年11月1日火曜日
2016年10月31日月曜日
Android FileWriter,FileReader
Android FileWriter,FileReader
文件的保存位置有要求
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Test();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void Test() throws IOException {
final String tag = "testFile";
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
FileWriter fileWriter = new FileWriter(path);
for( int i = 0; i <102; i++){
fileWriter.write(String.valueOf(i));
}
fileWriter.flush();
fileWriter.close();
FileReader fileReader = new FileReader(path);
char[] buf = new char[24];
int num;
while(((num = fileReader.read(buf)) != 0)){
System.out.println("---" + new String(buf,0,num));
}
fileReader.close();
}
}
文件的保存位置有要求
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Test();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void Test() throws IOException {
final String tag = "testFile";
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
FileWriter fileWriter = new FileWriter(path);
for( int i = 0; i <102; i++){
fileWriter.write(String.valueOf(i));
}
fileWriter.flush();
fileWriter.close();
FileReader fileReader = new FileReader(path);
char[] buf = new char[24];
int num;
while(((num = fileReader.read(buf)) != 0)){
System.out.println("---" + new String(buf,0,num));
}
fileReader.close();
}
}
结果
com.example.java.m1031a I/System.out: ---012345678910111213141516
com.example.java.m1031a I/System.out: ---171819202122232425262728
com.example.java.m1031a I/System.out: ---293031323334353637383940
com.example.java.m1031a I/System.out: ---414243444546474849505152
com.example.java.m1031a I/System.out: ---535455565758596061626364
com.example.java.m1031a I/System.out: ---656667686970717273747576
com.example.java.m1031a I/System.out: ---777879808182838485868788
com.example.java.m1031a I/System.out: ---899091929394959697989910
com.example.java.m1031a I/System.out: ---0101
2016年10月30日日曜日
Sublime Text 利用するプラグイン
Sublime Text 利用するプラグイン
1. PackageControl 追加
https://packagecontrol.io/installation#Simple
2. 日本語サポートする IMESupport
3. SFTP FTPサーバーと接続するため
4. F5 を押すと日付を入力する insertDate
SublimeAStyleFormatter
is a simple code formatter plugin for Sublime Text 2.
It provides ability to format C, C++, C#, and Java files.
1. PackageControl 追加
https://packagecontrol.io/installation#Simple
2. 日本語サポートする IMESupport
3. SFTP FTPサーバーと接続するため
4. F5 を押すと日付を入力する insertDate
SublimeAStyleFormatter
is a simple code formatter plugin for Sublime Text 2.
It provides ability to format C, C++, C#, and Java files.
Android應用開發視頻教程
黑马程序员 毕向东 Java基础视频教程
https://www.youtube.com/playlist?list=PLvswSo32Xlu_ctuWa-7-huGYUlsslzVhi
Android應用開發視頻教程
https://www.youtube.com/playlist?list=PLvswSo32Xlu8EVeS2RZvTg30M78WLROkT
https://www.youtube.com/playlist?list=PLvswSo32Xlu_ctuWa-7-huGYUlsslzVhi
Android應用開發視頻教程
https://www.youtube.com/playlist?list=PLvswSo32Xlu8EVeS2RZvTg30M78WLROkT
2016年10月27日木曜日
Grid View 自定义布局
Grid View 自定义布局
1. 选择 Grid View 的布局位置
2. 自定义Adapter
3. 设置数据
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView) findViewById(R.id.gv);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.grid_textview);
String s = tv.getText().toString();
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
});
imgAdapter imgadapter = new imgAdapter();
gv.setAdapter(imgadapter);
}
class imgAdapter extends BaseAdapter {
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(getApplication()).inflate(R.layout.gridview, null);
ImageView iv = (ImageView) v.findViewById(R.id.grid_img);
TextView tv = (TextView) v.findViewById(R.id.grid_textview);
iv.setImageResource(mThumbIds[position]);
tv.setText(getString(mThumbIds[position]));
return v;
}
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
}
Activity 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.e560.m1026gridview.MainActivity">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
自定义布局
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/grid_img"
android:layout_width="400dp"
android:layout_height="140dp"
android:background="#CCCCCC" />
<TextView
android:id="@+id/grid_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFCCCC"
android:textSize="20dp" />
</RelativeLayout>
1. 选择 Grid View 的布局位置
2. 自定义Adapter
3. 设置数据
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView) findViewById(R.id.gv);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.grid_textview);
String s = tv.getText().toString();
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
});
imgAdapter imgadapter = new imgAdapter();
gv.setAdapter(imgadapter);
}
class imgAdapter extends BaseAdapter {
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(getApplication()).inflate(R.layout.gridview, null);
ImageView iv = (ImageView) v.findViewById(R.id.grid_img);
TextView tv = (TextView) v.findViewById(R.id.grid_textview);
iv.setImageResource(mThumbIds[position]);
tv.setText(getString(mThumbIds[position]));
return v;
}
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
}
Activity 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.e560.m1026gridview.MainActivity">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
自定义布局
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/grid_img"
android:layout_width="400dp"
android:layout_height="140dp"
android:background="#CCCCCC" />
<TextView
android:id="@+id/grid_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFCCCC"
android:textSize="20dp" />
</RelativeLayout>
2016年10月24日月曜日
Android 生命周期
Android 生命周期
第一次启动
MainActivity: --1--onCreate
MainActivity: --2--onStart
MainActivity: --3--onResume
跳到其他Activity
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
返回键返回
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
接电话
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState 有延迟
挂电话
MainActivity: --3--onResume
按返回键推出到Android界面
MainActivity: --4--onPause
MainActivity: --5--onStop
MainActivity: --7--onDestroy
重新启动
MainActivity: --1--onCreate
MainActivity: --2--onStart
MainActivity: --3--onResume
旋转屏幕
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
MainActivity: --7--onDestroy
MainActivity: --1--onCreate
MainActivity: --2--onStart
MainActivity: --8--onRestoreInstanceState
MainActivity: --3--onResume
按Home键
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
按APP图标开启
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
按Power键,关屏幕
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
按Power键,开屏幕
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
按方块键
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
按方块键,重新回到App
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "--1--onCreate");
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "--2--onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "--3--onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "--4--onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "--5--onStop");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "--6--onRestart");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "--7--onDestroy");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "--8--onRestoreInstanceState");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG,"--9--onSaveInstanceState");
}
}
第一次启动
MainActivity: --1--onCreate
MainActivity: --2--onStart
MainActivity: --3--onResume
跳到其他Activity
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
返回键返回
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
接电话
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState 有延迟
挂电话
MainActivity: --3--onResume
按返回键推出到Android界面
MainActivity: --4--onPause
MainActivity: --5--onStop
MainActivity: --7--onDestroy
重新启动
MainActivity: --1--onCreate
MainActivity: --2--onStart
MainActivity: --3--onResume
旋转屏幕
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
MainActivity: --7--onDestroy
MainActivity: --1--onCreate
MainActivity: --2--onStart
MainActivity: --8--onRestoreInstanceState
MainActivity: --3--onResume
按Home键
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
按APP图标开启
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
按Power键,关屏幕
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
按Power键,开屏幕
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
按方块键
MainActivity: --4--onPause
MainActivity: --9--onSaveInstanceState
MainActivity: --5--onStop
按方块键,重新回到App
MainActivity: --6--onRestart
MainActivity: --2--onStart
MainActivity: --3--onResume
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "--1--onCreate");
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "--2--onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "--3--onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "--4--onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "--5--onStop");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "--6--onRestart");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "--7--onDestroy");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG, "--8--onRestoreInstanceState");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG,"--9--onSaveInstanceState");
}
}
2016年10月22日土曜日
ListFragment 和自定义布局Layout
ListFragment 和自定义布局Layout
public class ListFrag extends ListFragment {
public myadaputer adapter;
public List<String> data;
public void setData(List<String> data) {
this.data = data;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new myadaputer();
adapter.setDate(getdata());
}
public List<String> getdata(){
Bundle ig = getArguments();
int size = ig.getInt("size");
List<String> x = new ArrayList<String>();
for(int i=0;i< size ;i++) {
x.add("add" + i);
}
return x;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listfrag_layout, null);
setListAdapter(adapter);
return view;
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "onListItemClick", Toast.LENGTH_SHORT).show();
}
class myadaputer extends BaseAdapter {
private List<String> data;
public void setDate(List<String> data) {
this.data = data;
}
@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) {
View view = null;
if(convertView == null){
view = LayoutInflater.from(getActivity()).inflate(R.layout.listview,null);
}else{
view = convertView;
}
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(data.get(position).toString());
return view;
}
}
}
public class ListFrag extends ListFragment {
public myadaputer adapter;
public List<String> data;
public void setData(List<String> data) {
this.data = data;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new myadaputer();
adapter.setDate(getdata());
}
public List<String> getdata(){
Bundle ig = getArguments();
int size = ig.getInt("size");
List<String> x = new ArrayList<String>();
for(int i=0;i< size ;i++) {
x.add("add" + i);
}
return x;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listfrag_layout, null);
setListAdapter(adapter);
return view;
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "onListItemClick", Toast.LENGTH_SHORT).show();
}
class myadaputer extends BaseAdapter {
private List<String> data;
public void setDate(List<String> data) {
this.data = data;
}
@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) {
View view = null;
if(convertView == null){
view = LayoutInflater.from(getActivity()).inflate(R.layout.listview,null);
}else{
view = convertView;
}
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(data.get(position).toString());
return view;
}
}
}
登録:
投稿 (Atom)