每日一文APP实现(网页json解析)

本文介绍了如何使用Android实现每日一文应用,通过HTTP请求获取远程JSON数据,解析并展示文章的标题、作者、内容和时间。代码包括MainActivity.java、activity_main.xml和AndroidManifest.xml,涉及网络权限、数据存储和UI布局。
摘要由CSDN通过智能技术生成

hello,又是我鑫鑫

今天给大家带来每日一文APP的实现,从中会用到网页json解析,这里懂的都懂啊,这篇文章多多少少对你也有点启发,那么话不多说,直接看代码吧

-MainActivity.java

- activity_main.xml

- AndroidManifest.xml

效果如下

图片效果

/-------------------------------------/

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"); //data Json 对象
						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);


						/*JSONArray jsonArrayWeek = dataObject.getJSONArray("week"); //week 是一个json 数组
						 for (int i = 0; i < jsonArrayWeek.length(); i++) {
						 String week = jsonArrayWeek.get(i).toString();
						 Log.i("第三组数据:","week:" + week );
						 }*/
					} 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(); //create folders where write files
				
				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"/>

    <!--修改或删除您的SD卡中的内容-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!--访问SD卡文件系统-->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

    <!--读取您的SD卡中的内容-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

/-------------------------------------/

结尾

这个项目应该也没什么难度吧,感兴趣的小伙伴可以试一试,这里我就不放在Gitee或者GitHub上面了,毕竟这已经是全部代码了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值