获取视图
downImg = (Button) findViewById(R.id.down_img);
readImg = (Button) findViewById(R.id.read_img);
downJson = (Button) findViewById(R.id.down_json);
readJson = (Button) findViewById(R.id.read_json);
img = (ImageView) findViewById(R.id.img);
设置监听 开启异步任务下载
downJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyjsonAsyncTask(MainActivity.this).execute("https://gitee.com/little_bird_oh_777/test_data_collection/raw/master/message.json");
}
});
readJson.setOnClickListener(new View.OnClickListener() {
StringBuilder stringBuilder=new StringBuilder();
@Override
public void onClick(View v) {
try {
FileInputStream fileInputStream = openFileInput("json.txt");
byte[] bys=new byte[1024];
int len=0;
while ((len=fileInputStream.read(bys))!=-1){
stringBuilder.append(new String(bys,0,len));
}
Log.i("---",stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
downImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyimgAsyncTask(MainActivity.this,img).execute("https://c-ssl.duitang.com/uploads/item/201711/05/20171105160320_ABZrK.jpeg");
}
});
readImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
进行网络URL链接 从path下载字符串
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(3000);
urlConnection.setConnectTimeout(3000);
urlConnection.connect();
if(urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
byte[] bys=new byte[1024];
int len=0;
while((len=inputStream.read(bys))!=-1){
stringBuilder.append(new String(bys,0,len));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return stringBuilder.toString();
onPostExecute方法获取字符串
写入文件
try {
FileOutputStream fileOutputStream = activity.openFileOutput("json.txt", Context.MODE_PRIVATE);
fileOutputStream.write(s.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}```
### 下载图片 写入文件
``
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if(urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
byte[] bys=new byte[1024];
int len=0;
while((len=inputStream.read(bys))!=-1){
// stringBuilder.append(new String(bys,0,len));
by.write(bys,0,len);
}
}
} catch (Exception e) {
e.printStackTrace();
}
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.i("---","=======");
try {
FileOutputStream fileOutputStream = activity.openFileOutput("img.png", Context.MODE_PRIVATE);
fileOutputStream.write(by.toByteArray());
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
bitmap 填充视图
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = Environment.getExternalStorageDirectory();
File file1=new File(file,"img.png");
bitmap= BitmapFactory.decodeFile(file1.getAbsolutePath());
img.setImageBitmap(bitmap);
}```