Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 #881 第六次实验 #2311

Merged
merged 3 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion soft1614080902117/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Soft1614080902117Activity1"></activity>
<activity android:name=".Soft1614080902117Activity1" />
<activity android:name=".Soft1614080902117Activity2" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package edu.hzuapps.androidlabs.soft1614080902117;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.app.Activity;

import android.widget.ListView;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.String;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Soft1614080902117Activity extends Activity {
String[] adapterData;
Expand All @@ -39,6 +34,14 @@ public void onClick(View v) {
thisActivity.startActivity(intent);
}
});
Button btnDlow = (Button)findViewById(R.id.button8);
btnDlow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(thisActivity,Soft1614080902117Activity2.class);
thisActivity.startActivity(intent);
}
});
}
public String load(){ //读取文件
FileInputStream in =null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package edu.hzuapps.androidlabs.soft1614080902117;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class Soft1614080902117Activity2 extends Activity {
private EditText editText;
private Button button;
private ImageView imageView;
private Bitmap bitmap;
Handler handler=new Handler(){
public void handleMessage(Message msg) {
if(msg.what==111){
imageView.setImageBitmap(bitmap);
}
};
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft16140809021172);
editText=(EditText) findViewById(R.id.editweb);
button=(Button) findViewById(R.id.button7);
imageView=(ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Thread(t).start();
}
});
}
Thread t=new Thread(){
public void run() {
String iPath=editText.getText().toString();
try {
URL url=new URL(iPath);
InputStream inputStream=url.openStream();
bitmap=BitmapFactory.decodeStream(inputStream);
handler.sendEmptyMessage(111);
inputStream.close();
inputStream=url.openStream();
File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/");
FileOutputStream fileOutputStream=new FileOutputStream(file);
int hasRead=0;
while((hasRead=inputStream.read())!=-1){
fileOutputStream.write(hasRead);
}
fileOutputStream.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@

</SearchView>

<ListView
android:layout_width="346dp"
android:layout_height="426dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.493" />
<TextView
android:id="@+id/textView"
android:layout_width="363dp"
Expand All @@ -58,6 +44,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintVertical_bias="0.516" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
Expand All @@ -72,24 +59,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.781"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button2"
app:layout_constraintVertical_bias="1.0" />

<Button
android:id="@+id/button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="68dp"
android:layout_marginRight="8dp"
android:layout_marginStart="68dp"
android:background="@drawable/xinjian"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.026"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintVertical_bias="1.0" />

<TextView
Expand All @@ -104,7 +74,7 @@
android:text="新建"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.027"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.997" />
Expand All @@ -113,20 +83,66 @@
android:id="@+id/button2"
android:layout_width="27dp"
android:layout_height="25dp"
android:layout_marginBottom="4dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/shezhi"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.678"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.975" />
app:layout_constraintHorizontal_bias="0.779"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="68dp"
android:layout_marginRight="8dp"
android:layout_marginStart="68dp"
android:background="@drawable/xinjian"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />

<ListView
android:id="@+id/listView"
android:layout_width="346dp"
android:layout_height="404dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</ListView>

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button8"
android:layout_width="32dp"
android:layout_height="23dp"
android:layout_marginBottom="16dp"
android:background="@android:drawable/arrow_down_float"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".Soft1614080902117Activity2">

<TextView
android:id="@+id/textView8"
android:layout_width="369dp"
android:layout_height="53dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:background="@drawable/background1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.48"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView12"
android:layout_width="65dp"
android:layout_height="22dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="网络下载"
app:layout_constraintEnd_toEndOf="@+id/textView8"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView8" />

<EditText
android:id="@+id/editweb"
android:layout_width="279dp"
android:layout_height="wrap_content"

android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView8" />

<Button
android:id="@+id/button7"
android:layout_width="68dp"
android:layout_height="36dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:background="#dfd9d9"
android:text="下载"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"

app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="352dp"
android:layout_height="408dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>
43 changes: 43 additions & 0 deletions soft1614080902117/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 第六次实验

## 一、实验目标

1. 掌握Android网络访问方法;
2. 理解XML和JSON表示数据的方法。

## 二、实验内容(选做一)

1. 在个人目录中创建一个表示数据的XML或JSON文件;
2. 数据文件代码提交之后从GitHub获取文件URL;
3. 在应用中通过网络编程访问GitHub的数据文件;
4. 在应用中解析并显示文件所包含的数据;
5. 将应用运行结果截图。

## 实验内容(选做一)

1. 从网络下载一个文件(图片、MP3、MP4);
2. 保存到手机,在应用中使用文件;
3. 将应用运行结果截图。

## 三、实验步骤
1.在主类Soft1614080902117Activity中添加一个下载按钮,点击可跳转至Soft1614080902117Activity2
进入图片下载界面;

2.在activity_soft16140809021172.xml中添加EditText控件,使得在EditText中可输入图片下载链接,
同时添加下载按钮,并添加监听,点击可进行下载并用ImageView显示图片;

3.在Soft1614080902117Activity2中添加子线程Thread t=new Thread();使得可进行多次下载,同时将
EditText中获取的图片链接用String iPath=editText.getText().toString();进行获取再用
URL url=new URL(iPath);进行链接,再用InputStream inputStream=url.openStream();打开输出流
将图片下载转换后关闭输出流inputStream.close();

## 四、实验截图
主界面
![image](https://github.com/1614080902117/android-labs-2018/blob/master/soft1614080902117/shiyan6.1.png)
下载
![image](https://github.com/1614080902117/android-labs-2018/blob/master/soft1614080902117/shiyan6.2.png)
## 五、实验体会
此次实验是通过参考再根据自己需求修改完成的,也大致的了解了Android下载的大概流程,
获取链接进行连接和转换下载和显示。原本是选做第一个解析JSON文件,但实现过程中不知道
如何在Android中添加jsoup。

Binary file added soft1614080902117/shiyan6.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902117/shiyan6.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.