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

Unity script generates configurable files and loads

【pyqt】tableWidget里的cellWidget使用信号与槽机制
![[machine learning 03] Lagrange multiplier method](/img/14/7d4eb5679606e272f137ddbda4938c.png)
[machine learning 03] Lagrange multiplier method
![[untitled]](/img/c7/b6abe0e13e669278aea0113ca694e0.jpg)
[untitled]

Antd select selector drop-down box follows the scroll bar to scroll through the solution

Avoid mutating a prop directly since the value will be overwritten whenever the parent component

Deep understanding of Apache Hudi asynchronous indexing mechanism

Socket socket programming
![[untitled]](/img/15/3db921703147afdf58dfffe532a19b.jpg)
[untitled]

Web端自动化测试失败的原因
随机推荐
What if copying is prohibited?
[installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
V-for img SRC rendering fails
Deconstruction and assignment of variables
How to remove addition and subtraction from inputnumber input box
How to play video on unityui
2021-04-23
Get pictures through opencv, change channels and save them
The fifth training assignment
The seventh training assignment
Shardingsphere sub database and table examples (logical table, real table, binding table, broadcast table, single table)
uniapp 在onLaunch中跳转页面后,点击事件失效解决方法
Deeply understand the characteristics of database transaction isolation
Process control (creation, termination, waiting, program replacement)
Socket socket programming
[untitled]
【pyqt】tableWidget里的cellWidget使用信号与槽机制
Kitex 重试机制
2021 summary and 2022 outlook
How to use cherry pick?