当前位置:网站首页>Case study of Jinshan API translation function based on retrofit framework
Case study of Jinshan API translation function based on retrofit framework
2022-07-07 11:14:00 【Brick factory worker】
be based on Retrofit Golden Mountain of framework API Translation function cases
the API as follows , You can use a browser or postman see .
https://dict-mobile.iciba.com/interface/index.php?c=word&m=getsuggest&nums=1&is_need_mean=1&word=hello
The above is translation hello Word meaning , If you need to translate others , Only need to change word=hello The value of the can .
This visit uses get request , Return to one Json character string , Also on Json The string is deserialized and converted to JavaBean. in addition JavaBean You can use the website to automatically generate , Enter the returned Json Object can . Address the following :
Back to json The contents are as follows :
{
"message": [
{
"key": "hello",
"paraphrase": "n.“ feed ” Hello or hello ;int. hello , feed , Hello , Hello! , Express greetings , Say hello ",
"value": 0,
"means": [
{
"part": "int.",
"means": [
" hello , feed ",
" Hello , Hello! ",
" Express greetings ",
" Say hello "
]
},
{
"part": "n.",
"means": [
"“ feed ” Hello or hello "
]
}
]
}
],
"status": 1
}
design sketch :
Case code :
Third party libraries that need to be relied on
// stay build.gradle Under the document implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
AndroidManifest.xml Open network access in
<uses-permission android:name="android.permission.INTERNET"/>
JavaBean:
Translation.java
public class Translation { private List<Message> message; private int status; public void setMessage(List<Message> message) { this.message = message; } public List<Message> getMessage() { return message; } public void setStatus(int status) { this.status = status; } public int getStatus() { return status; } @Override public String toString() { return "Translation{" + "message=" + message + ", status=" + status + '}'; } }
Message.java
public class Message { private String key; private String paraphrase; private int value; private List<Means> means; public void setKey(String key) { this.key = key; } public String getKey() { return key; } public void setParaphrase(String paraphrase) { this.paraphrase = paraphrase; } public String getParaphrase() { return paraphrase; } public void setValue(int value) { this.value = value; } public int getValue() { return value; } public void setMeans(List<Means> means) { this.means = means; } public List<Means> getMeans() { return means; } @Override public String toString() { return "Message{" + "key='" + key + '\'' + ", paraphrase='" + paraphrase + '\'' + ", value=" + value + ", means=" + means + '}'; } }
Means.java
public class Means { private String part; private List<String> means; public void setPart(String part) { this.part = part; } public String getPart() { return part; } public void setMeans(List<String> means) { this.means = means; } public List<String> getMeans() { return means; } @Override public String toString() { return "Means{" + "part='" + part + '\'' + ", means=" + means + '}'; } }
Custom request interface :GetRequest_interface
public interface GetRequest_interface { @GET("interface/index.php?c=word&m=getsuggest&nums=1&is_need_mean=1") Call<Translation> getCall(@Query("word") String word); }
MainActivity.java
public class MainActivity extends AppCompatActivity { private EditText et; private Button btn; private TextView tv; private Handler handler = new Handler(Looper.getMainLooper()); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = findViewById(R.id.btn_trans); tv = findViewById(R.id.result); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // There's a little pit here , When you want to extract TextView The object is , Need to put findViewById() Put it in the click event et = findViewById(R.id.et_word); String content = et.getText().toString().trim(); request(content); } }); } private void request(String str) { new Thread(new Runnable() { @Override public void run() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://dict-mobile.iciba.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); GetRequest_interface request = retrofit.create(GetRequest_interface.class); Call<Translation> call = request.getCall(str); Response<Translation> response = null; try { response = call.execute(); } catch (IOException e) { e.printStackTrace(); } Translation translation = response.body(); String result = translation.getMessage().get(0).getParaphrase(); handler.post(new Runnable() { @Override public void run() { tv.setText(result); } }); } }).start(); } }
Layout interface :activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/et_word" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint=" Please enter the words that need to be translated :" /> <Button android:id="@+id/btn_trans" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text=" translate " /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" android:text=" Translation results :" android:textColor="@color/black" android:textSize="20sp" /> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" /> </LinearLayout>
边栏推荐
- [untitled]
- Kitex 重试机制
- Opencv installation and environment configuration - vs2017
- Bookmarking - common website navigation for programmers
- 在我有限的软件测试经历里,一段专职的自动化测试经验总结
- mif 文件格式记录
- Unity downloads files through the server address
- Transaction rolled back because it has been marked as rollback-only解决
- A case of compiling QT file qmake compiling script
- Typescript interface inheritance
猜你喜欢
MPX plug-in
Unity script generates configurable files and loads
面试被问到了解哪些开发模型?看这一篇就够了
[untitled]
Ping tool ICMP message learning
[untitled]
[untitled]
Deeply understand the characteristics of database transaction isolation
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
Verilog 实现数码管显视驱动【附源码】
随机推荐
VIM命令模式与输入模式切换
[untitled]
Add a self incrementing sequence number to the antd table component
Force buckle 1002 Find common characters
创意信息获2家机构调研:GreatDB 数据库已在9地部署
[untitled]
基于Retrofit框架的金山API翻译功能案例
[untitled]
[untitled]
“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
解决VSCode只能开两个标签页的问题
毕业季|与青春作伴,一起向未来!
关于测试人生的一站式发展建议
[untitled]
Project ERROR: Unknown module(s) in QT: core gui
Process control (creation, termination, waiting, program replacement)
测试开发基础,教你做一个完整功能的Web平台之环境准备
[STM32] actual combat 3.1 - drive 42 stepper motors with STM32 and tb6600 drivers (I)
[untitled]