当前位置:网站首页>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 :

Json turn JavaBean

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 :

  1. 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'
    
  2. AndroidManifest.xml Open network access in

    <uses-permission android:name="android.permission.INTERNET"/>
    
  3. 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 +
                    '}';
        }
    }
    
  4. 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);
    }
    
  5. 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();
        }
    }
    
  6. 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>
    
原网站

版权声明
本文为[Brick factory worker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070904297302.html