当前位置:网站首页>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>
边栏推荐
猜你喜欢

普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
![[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk](/img/41/3a9450a84291ba04caee65241bce5d.png)
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk

关于测试人生的一站式发展建议

【C#】WinForm运行缩放(变糊)的解决方法

uniCloud

关于jmeter中编写shell脚本json的应用

技术分享 | 抓包分析 TCP 协议

高考作文,高频提及科技那些事儿……

【pyqt】tableWidget里的cellWidget使用信号与槽机制

2021 summary and 2022 outlook
随机推荐
[C #] the solution of WinForm operation zoom (blur)
基于STC8G1K08的0.96寸IIC液晶屏驱动程序
创意信息获2家机构调研:GreatDB 数据库已在9地部署
Use of dotween
MPX plug-in
July 10, 2022 "five heart public welfare" activity notice + registration entry (two-dimensional code)
Long list performance optimization scheme memo
2022.7.4DAY596
A case of compiling QT file qmake compiling script
PostgreSQL中的表复制
uniapp 在onLaunch中跳轉頁面後,點擊事件失效解决方法
Kitex retry mechanism
Mpx 插件
Socket socket programming
[untitled]
Ffmpeg record a video command from RTSP
[untitled]
Ping tool ICMP message learning
uniCloud
解决VSCode只能开两个标签页的问题