hello,又是我鑫鑫
今天给大家带来每日一文APP的实现,从中会用到网页json解析,这里懂的都懂啊,这篇文章多多少少对你也有点启发,那么话不多说,直接看代码吧
效果如下
/-------------------------------------/
MainActivity.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.mycompany.myapp.R;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Date;
import java.text.SimpleDateFormat;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
private TextView t,a,c,ti;
LinearLayout Get_tips,Get_results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Article_acquisition();
t = findViewById(R.id.title);
a = findViewById(R.id.author);
c = findViewById(R.id.content);
ti = findViewById(R.id.time);
Get_tips = findViewById(R.id.Get_tips);
Get_results = findViewById(R.id.Get_results);
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Get_tips.setVisibility(View.GONE);
if(Get_tips.getVisibility()==View.GONE){
Get_results.setVisibility(View.VISIBLE);
SharedPreferences sharedPreferences = getSharedPreferences("每日一文",MODE_PRIVATE);
String title = sharedPreferences.getString("标题","");
t.setText(title);
String author = sharedPreferences.getString("作者","");
a.setText(author);
String content = sharedPreferences.getString("内容","");
c.setText(content);
String time = sharedPreferences.getString("时间","");
ti.setText(time);
}
}
}, 4000);
}
private void Article_acquisition(){
new Thread() {
@Override
public void run() {
try {
HttpURLConnection connection = null;
URL url = new URL("http://ovooa.com/API/mryw/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine())!=null){
sb.append(line);
}
Log.d("MainActivity"," "+sb.toString());
try {
JSONObject jsonObject = new JSONObject(sb.toString());
String num = jsonObject.getString("code");
String progectName = jsonObject.getString("text");
Log.i("第一组数据::","code: " + num + "\n"
+ "text: " + progectName
);
JSONObject dataObject = jsonObject.getJSONObject("data");
final String title = dataObject.getString("title");
final String author = dataObject.getString("author");
final String content = dataObject.getString("content");
final String time = dataObject.getString("time");
Log.i("第二组数据:", title);
Log.i("第二组数据:",author);
Log.i("第三组数据:",content);
Log.i("第四组数据:",time);
SharedPreferences.Editor editor = getSharedPreferences("每日一文",MODE_PRIVATE).edit();
editor.putString("标题",title);
editor.putString("作者",author);
editor.putString("内容",content);
editor.putString("时间",time)
.apply();
saveToSDCard(title + "\n" + author + "\n" + content + "\n" + time);
} catch (JSONException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public static File dir = new File(Environment.getExternalStorageDirectory() + "/鑫鑫工具箱/每日一文/");
public static void saveToSDCard(String content) {
String en = Environment.getExternalStorageState();
if (en.equals(Environment.MEDIA_MOUNTED)) {
try {
dir.mkdirs();
File file = new File(Environment.getExternalStorageDirectory() + "/鑫鑫工具箱/每日一文/" + getFileName() + ".txt");
OutputStream out = new FileOutputStream(file);
out.write(content.getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
}
public static String getFileName() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date = format.format(new Date(System.currentTimeMillis()));
return date;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_height="100dp"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="每日一文"
android:textStyle="bold"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:text="后续将出分享文章的功能"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/Get_tips">
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:text="文章获取中……"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/Get_results"
android:visibility="gone">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:text="标题"
android:id="@+id/title"
android:gravity="center"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:text="作者"
android:id="@+id/author"
android:gravity="center"/>
<View
android:background="?android:attr/dividerVertical"
android:layout_height="1dp"
android:layout_width="match_parent"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:text="内容"
android:id="@+id/content"/>
<View
android:background="?android:attr/dividerVertical"
android:layout_height="1dp"
android:layout_width="match_parent"/>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:text="时间"
android:gravity="center"
android:id="@+id/time"/>
</LinearLayout>
<View
android:layout_height="0dp"
android:layout_width="match_parent"/>
</LinearLayout>
</ScrollView>
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
/-------------------------------------/
结尾
这个项目应该也没什么难度吧,感兴趣的小伙伴可以试一试,这里我就不放在Gitee或者GitHub上面了,毕竟这已经是全部代码了。