Hello,我是鑫鑫
这次给大家带来的是百度百科APP制作源码
原理和我上一篇文章差不多
这篇文章就不多说什么了,直接给大家gItee仓库吧
gitee代码
https://gitee.com/wwwyxyx/baidu-encyclopedia-app.git
效效果如下
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
Button Search;
EditText Content;
TextView SC_M,SC_In,SC_Im,SC_U;
ImageView picture;
LinearLayout Get_tips,Get_results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Get_tips = findViewById(R.id.Get_tips);
Get_results = findViewById(R.id.Get_results);
SC_M = findViewById(R.id.SC_M);
SC_In = findViewById(R.id.SC_In);
SC_Im = findViewById(R.id.SC_Im);
SC_U = findViewById(R.id.SC_U);
SC_U.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPreferences = getSharedPreferences("百度百科结果",MODE_PRIVATE);
String Url = sharedPreferences.getString("地址","");
Uri uri = Uri.parse(Url); //设置跳转的网站
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Content = findViewById(R.id.Content);
Search = findViewById(R.id.Search);
Search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Get_tips.setVisibility(View.VISIBLE);
String Search_Content = Content.getText().toString();
SharedPreferences.Editor editor = getSharedPreferences("百度百科内容",MODE_PRIVATE).edit();
editor.clear();
editor.putString("内容",Search_Content)
.apply();
SharedPreferences sharedPreferences = getSharedPreferences("百度百科结果",MODE_PRIVATE);
sharedPreferences.edit().clear().commit();
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
Article_acquisition();
}
}, 2000);
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 Msg = sharedPreferences.getString("内容","");
String info = sharedPreferences.getString("信息","");
String image = sharedPreferences.getString("图片","");
String Url = sharedPreferences.getString("地址","");
SC_M.setText(Msg);
SC_In.setText(info);
SC_Im.setText(image);
SC_U.setText(Url);
picture = findViewById(R.id.ImageView);
Glide.with(MainActivity.this)
.load(image)
.into(picture);
}
}
}, 4000);
}
});
}
private void Article_acquisition(){
new Thread() {
@Override
public void run() {
try {
HttpURLConnection connection = null;
SharedPreferences sharedPreferences = getSharedPreferences("百度百科内容",MODE_PRIVATE);
String SJNN = sharedPreferences.getString("内容","");
URL url = new URL("http://ovooa.com/API/bdbk/?Msg=" + SJNN);
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()); //整体文件是一个对象
JSONObject dataObject = jsonObject.getJSONObject("data"); //data Json 对象
final String Msg = dataObject.getString("Msg");
final String info = dataObject.getString("info");
final String image = dataObject.getString("image");
final String Url = dataObject.getString("url");
/* Log.i("第二组数据:", Msg);
Log.i("第二组数据:",info);
Log.i("第三组数据:",image);
Log.i("第四组数据:",Url);*/
SharedPreferences.Editor editor = getSharedPreferences("百度百科结果",MODE_PRIVATE).edit();
editor.clear();
editor.putString("内容",Msg);
editor.putString("信息",info);
editor.putString("图片",image);
editor.putString("地址",Url)
.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();
}
}
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/textAppearanceSmall"
android:layout_width="wrap_content"
android:text="©2022 xinToolbox. All rights reserved."/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ems="10"
android:id="@+id/Content"
android:layout_weight="1.0"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="百科"
android:id="@+id/Search"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/Get_tips"
android:visibility="gone">
<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">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:id="@+id/SC_M"/>
<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:id="@+id/SC_In"/>
<View
android:background="?android:attr/dividerVertical"
android:layout_height="1dp"
android:layout_width="match_parent"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1.0">
<TextView
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:id="@+id/SC_Im"/>
</ScrollView>
<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/ImageView"
android:layout_weight="1.0"/>
</LinearLayout>
<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:id="@+id/SC_U"/>
<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="wrap_content"
android:text="链接可点击"
android:textStyle="bold"
android:layout_gravity="center"/>
</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"/>
还有别忘了在APP下的build.gradle添加下方依赖
//Glide 加载图像
implementation 'com.github.bumptech.glide:glide:4.8.0'