垃圾分类APP(三)搜索功能的实现
搜索界面的布局
详情见之前文章!!!
准备工作
需要垃圾分类查询的接口
前往天行数据,注册账号,申请接口
前往个人中心,找到我的密钥,那个就是你用来调用接口的密钥,每人每天可以免费调用接口100次
加入约束
要用到 OKhttp3 所以前往 build.gradle 添加依赖
implementation 'com.squareup.okhttp3:okhttp:4.4.0'
implementation 'com.squareup.okio:okio:2.4.3'
新建类来使用Okhttp
一、新建一个API包来放和api相关的类
二、在API包下新建一个 OkhttpUntils 类
package com.example.goodrubish.API;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class OkhttpUntils {
public static void OkHttpPost(String url, String json, Callback callback){
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json);
Request request = new Request.Builder().url(url).post(body).build();
client.newCall(request).enqueue(callback);
}
public static void OkHttpGet(String url, Callback callback){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(callback);
}
}
三、在API包下新建一个 Okhttp_work 类
package com.example.goodrubish.API;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class Okhttp_work {
public static void OkHttpGet(String url, Callback callback){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(callback);
}
}
网络配置
需要手机的网络等权限,所以前往 AndroidManifest.xml 添加
<uses-permission android:name="android.permission.INTERNET" /> <!--网络-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <!--录音-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--文件读写-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" /> <!--相机-->
android:networkSecurityConfig="@xml/network_config"
android:requestLegacyExternalStorage="true"
下面报红了,因为我们还没有建网络配置
在res下新建一个xml文件夹,文件夹下新建 network_config.xml,里面代码如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
在xml文件夹下新建一个file_paths.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="" />
</paths>
网络配置完成啦!!!
处理接口传递过来的数据
浅看一下接收的数据
新建搜索栏放置一种垃圾
新建一个 garbage_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/garbage_name"
android:layout_width="245dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_gravity="center_vertical"
android:layout_marginTop="0dp"
android:gravity="center_vertical"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="20dp" />
<TextView
android:id="@+id/garbage_type"
android:layout_width="165dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginTop="0dp"
android:layout_marginEnd="-6dp"
android:layout_marginRight="-6dp"
android:gravity="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</LinearLayout>
新建Garbge包
一、新建 Grabge_list 类处理接收的数据
package com.example.goodrubish.Garbage;
import com.example.goodrubish.Fragment.SearchFragment;
import org.json.JSONException;
import org.json.JSONObject;
public class Garbage_list {
private JSONObject search_result= SearchFragment.Search_JSON;
private String name;
private int garbage_type; //0是可回收,1是有害垃圾,2是厨余垃圾,3是其它垃圾
private String[] garbage_type_string={"可回收垃圾","有害垃圾","厨余垃圾","其它垃圾"};
public Garbage_list(JSONObject the_garbage_JSON) throws JSONException {
name=the_garbage_JSON.getString("name");
garbage_type=Integer.parseInt(the_garbage_JSON.getString("type"));
}
public String getName(){
return name;
}
public String getType(){
return garbage_type_string[garbage_type];
}
}
二、再建一个垃圾适配器 Garbage_adapter 类
package com.example.goodrubish.Garbage;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.goodrubish.R;
import java.util.List;
public class Garbage_adapter extends ArrayAdapter {
private int resourceId;
public Garbage_adapter(Context context, int textViewResourceId, List<Garbage_list> objects){
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Garbage_list garbage_list= (Garbage_list) getItem(position);
View view= LayoutInflater.from(getContext()).inflate(resourceId, parent,false);
TextView garbageName=(TextView)view.findViewById(R.id.garbage_name);
TextView garbageType=(TextView)view.findViewById(R.id.garbage_type);
garbageName.setText(garbage_list.getName());
garbageType.setText(garbage_list.getType());
return view;
}
}
实现查找分类功能
一、先到SearchFragment添加要用到的属性
private EditText et_search;
private Button bt_search;
public static JSONObject Search_JSON;
//接口地址加密钥
String url_garbage="http://api.tianapi.com/txapi/lajifenlei/index?key=****************************&word=";
//星号位置为你们自己的密钥,把数据换上去就行了
同之前一样在 SearchFragment 中加入下面代码
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
et_search = getActivity().findViewById(R.id.et_search);
bt_search = getActivity().findViewById(R.id.bt_search);
bt_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intn_search();
}
});
}
public void intn_search() {
View view = getActivity().getWindow().peekDecorView();
if (view != null) {//这里是网络访问的代码,获取服务
InputMethodManager inputmanger = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
initData_search();
}
Handler handler_search=new Handler(new Handler.Callback() {//首先通过handler发送信息
@Override
public boolean handleMessage(@NonNull Message msg) {
switch (msg.what){
case 0:
Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_LONG).show();
break;
case 1:
String json=(String)msg.obj;
try{
Search_JSON=new JSONObject(json);
Intent intent=new Intent();
intent.setClass(getActivity(), Search.class);
startActivity(intent); //进行页面跳转
break;
}catch (JSONException e){
e.printStackTrace();
}
default:
throw new IllegalStateException("Unexpected value: " + msg.what);
}
return false;
}
});
private void initData_search(){
String data=et_search.getText().toString();
if(TextUtils.isEmpty(data)){//如果内容为空,弹出toast消息,不为空则执行164行以后的代码
Toast.makeText(getActivity(),"垃圾名称不能为空!",Toast.LENGTH_SHORT).show();
}
else{
Okhttp_work.OkHttpGet(url_garbage+data, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String content=response.body().string();
Message message=new Message();
message.what=1;
message.obj=content;
handler_search.sendMessageDelayed(message,1000);
}
});
}
}
上面页面跳转到另外一个页面,记得创建这个Activity
搜索页面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".Search"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:orientation="horizontal"
android:background="#CCCCCC">
<EditText
android:id="@+id/text_searchb"
android:layout_width="wrap_content"
android:layout_height="40sp"
android:layout_weight="6"
android:background="#FFFFFF"
android:layout_gravity="center"
android:textSize="25sp"/>
<Button
android:id="@+id/btn_searchb"
android:layout_width="wrap_content"
android:layout_height="50sp"
android:layout_weight="1"
android:layout_gravity="center"
android:text="搜索"
android:textSize="20sp"/>
</LinearLayout>
<ListView
android:id="@+id/search_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
效果如下
搜索的Java代码
类似上面,搬代码过来就行
一、先加要用到的属性
private ListView garbageList_View;
public static int garbage_id;
private JSONObject search_result= SearchFragment.Search_JSON; //用于接收MainActivity中的搜索结果
private List<Garbage_list> garbageList=new ArrayList<>();
private EditText text_search;
private Button btn_search;
String url_garbage="http://api.tianapi.com/txapi/lajifenlei/index?key=***************************&word=";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
ActionBar actionbar=getSupportActionBar();
if(actionbar!=null){
actionbar.hide();
}
text_search = findViewById(R.id.text_searchb);
btn_search = findViewById(R.id.btn_searchb);
try {
init_garbage_list();
} catch (JSONException e) {
e.printStackTrace();
}
Garbage_adapter adapter=new Garbage_adapter(Search.this, R.layout.garbage_item, garbageList);
ListView listView=findViewById(R.id.search_listview);
listView.setAdapter(adapter);
btn_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
init_search();
}
});
garbageList_View=findViewById(R.id.search_listview);
garbageList_View.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
garbage_id=i;
Intent intent=new Intent();
//跳转到细节页面
intent.setClass(Search.this, DetailActivity.class);
startActivity(intent);
}
});
}
private JSONObject make_the_JSON(int i) throws JSONException { //用于将JSON转换为字符串数组
JSONArray newslist=search_result.getJSONArray("newslist");
String[] name=new String[newslist.length()];
String newslistString=new String();
JSONObject the_garbage_JSON = new JSONObject(); //具体某一项的json ,可以通过getstring获得其中具体的项
newslistString=newslist.getString(i);
the_garbage_JSON=new JSONObject(newslistString);
// name[i]=the_garbage_JSON.getString("name");
return the_garbage_JSON;
}
private void init_garbage_list() throws JSONException {
JSONArray newslist=search_result.getJSONArray("newslist");
for(int i=0;i<newslist.length();i++){
Garbage_list the_garbage=new Garbage_list(make_the_JSON(i));
garbageList.add(the_garbage);
}
}
public void init_search() {
View view = getWindow().peekDecorView();
if (view != null) {//这里是网络访问的代码,获取服务
InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
initData_search();
}
Handler handler_search=new Handler(new Handler.Callback() {//首先通过handler发送信息
@Override
public boolean handleMessage(@NonNull Message msg) {
switch (msg.what){
case 0:
Toast.makeText(Search.this,"网络错误",Toast.LENGTH_LONG).show();
break;
case 1:
String json=(String)msg.obj;
try{
SearchFragment.Search_JSON=new JSONObject(json);
Intent intent=new Intent();
intent.setClass(Search.this, Search.class);
startActivity(intent); //进行页面跳转
finish();
break;
}catch (JSONException e){
e.printStackTrace();
}
default:
throw new IllegalStateException("Unexpected value: " + msg.what);
}
return false;
}
});
private void initData_search(){
String data=text_search.getText().toString();
if(TextUtils.isEmpty(data)){//如果内容为空,弹出toast消息,不为空则执行164行以后的代码
Toast.makeText(Search.this,"垃圾名称不能为空!",Toast.LENGTH_SHORT).show();
}
else{
Okhttp_work.OkHttpGet(url_garbage+data, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String content=response.body().string();
Message message=new Message();
message.what=1;
message.obj=content;
handler_search.sendMessageDelayed(message,1000);
}
});
}
}
搜索结果细节界面
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:orientation="vertical"
tools:context=".Detail">
<TextView
android:id="@+id/detail_garbage_name"
android:layout_width="198dp"
android:layout_height="203dp"
android:gravity="center"
android:text="无网络连接~"
android:textColor="@color/black"
android:textSize="25sp"
android:textStyle="italic|bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.03" />
<TextView
android:id="@+id/detail_garbage_type"
android:layout_width="173dp"
android:layout_height="124dp"
android:gravity="center"
android:text="无网络连接~"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.105"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.202" />
<TextView
android:id="@+id/detail_garbage_explain"
android:layout_width="379dp"
android:layout_height="123dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:text="暂时没有网络连接~"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
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.43" />
<TextView
android:id="@+id/detail_garbage_contain"
android:layout_width="379dp"
android:layout_height="148dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:text="暂时没有网络连接~"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
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.691" />
<TextView
android:id="@+id/detail_garbage_tips"
android:layout_width="379dp"
android:layout_height="147dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="暂时没有网络连接~"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
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.914" />
<ImageView
android:id="@+id/detail_garbage_picture"
android:layout_width="176dp"
android:layout_height="169dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.931"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.08" />
</androidx.constraintlayout.widget.ConstraintLayout>
随便试试,效果如下,很简陋,后面再来优化吧,没什么艺术细胞!!!
Java代码
private TextView garbage_name;
private TextView garbage_type;
private TextView garbage_explain;
private TextView garbage_contain;
private TextView garbage_tips;
private ImageView garbage_picture;
private JSONObject search_result=MainActivity.Search_JSON;
private int detail_garbage_id=SearchActivity.garbage_id;
private String[] garbage_type_string={"可回收垃圾","有害垃圾","厨余垃圾","其它垃圾"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ActionBar actionbar=getSupportActionBar();
if(actionbar!=null){
actionbar.hide();
}
garbage_name=findViewById(R.id.detail_garbage_name);
garbage_type=findViewById(R.id.detail_garbage_type);
garbage_explain=findViewById(R.id.detail_garbage_explain);
garbage_contain=findViewById(R.id.detail_garbage_contain);
garbage_tips=findViewById(R.id.detail_garbage_tips);
garbage_picture=findViewById(R.id.detail_garbage_picture);
try {
garbage_name.setText(make_the_String("name"));
} catch (JSONException e) {
e.printStackTrace();
}
try {
String s = garbage_type_string[Integer.parseInt(make_the_String("type"))];
//这是在插入图片,可以在网上找到自己想要的图片
switch (s){
case "可回收垃圾":
garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.recyclablewast));
break;
case "有害垃圾":
garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.harmfulwaste));
break;
case "厨余垃圾":
garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.kitchenwaste));
break;
case "其它垃圾":
garbage_picture.setBackgroundDrawable(getResources().getDrawable(R.drawable.otherwaste));
break;
}
garbage_type.setText(garbage_type_string[Integer.parseInt(make_the_String("type"))]);
} catch (JSONException e) {
e.printStackTrace();
}
try {
garbage_explain.setText(" "+make_the_String("explain"));
} catch (JSONException e) {
e.printStackTrace();
}
try {
garbage_contain.setText(" "+make_the_String("contain"));
} catch (JSONException e) {
e.printStackTrace();
}
try {
garbage_tips.setText(" 小贴士:"+make_the_String("tip"));
} catch (JSONException e) {
e.printStackTrace();
}
}
private String make_the_String(String need) throws JSONException { //用于将JSON转换为字符串数组
JSONArray newslist=search_result.getJSONArray("newslist");
// String[] name=new String[newslist.length()];
String newslistString=new String();
JSONObject the_garbage_JSON=new JSONObject(); //具体某一项的json ,可以通过getstring获得其中具体的项
newslistString=newslist.getString(detail_garbage_id);
the_garbage_JSON=new JSONObject(newslistString);
// name[i]=the_garbage_JSON.getString("name");
return the_garbage_JSON.getString(need);
}
好了,搜索功能也完事了,差个新闻功能可能也挺费脑子,其实这些都是我已经自己弄好的了,新闻还没成功搞好,拖更了,什么时候实验成功了再回来写。
参考文章
我可以也是学了别人的文章来弄的呀,你们也可以看看别人的文章
Android 垃圾分类APP(一)申请API、搭建项目、访问接口获取数据
从零开始用Andrid Studio开发一个简单的垃圾分类查询APP(3)