标签:ble 定义数据 iss appcompat word 发送 import blog interface
访问的url为:http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=hello
第一步:
在app下的build.gradle中添加
// Retrofit库 compile ‘com.squareup.retrofit2:retrofit:2.0.2‘ // Okhttp库 compile ‘com.squareup.okhttp3:okhttp:3.1.2‘ //Gson库 compile ‘com.squareup.retrofit2:converter-gson:2.0.2‘
第二步:
创建实体类:Translation
package com.retrofit.ych.retrofit.entity; public class Translation { private int status; private content content; private static class content{ private String ph_en; private String ph_am; private String ph_am_mp3; private String[] word_mean; public String getPh_en() { return ph_en; } public void setPh_en(String ph_en) { this.ph_en = ph_en; } public String getPh_am() { return ph_am; } public void setPh_am(String ph_am) { this.ph_am = ph_am; } public String getPh_am_mp3() { return ph_am_mp3; } public void setPh_am_mp3(String ph_am_mp3) { this.ph_am_mp3 = ph_am_mp3; } public String[] getWord_mean() { return word_mean; } public void setWord_mean(String[] word_mean) { this.word_mean = word_mean; } } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public Translation.content getContent() { return content; } public void setContent(Translation.content content) { this.content = content; } //定义数据返回数据的方法 public void show(){ System.out.println("ph_en"+content.ph_en); System.out.println("ph_am"+content.ph_am); System.out.println("ph_am_mp3"+content.ph_am_mp3); System.out.println("word_mean"+content.word_mean.length); for (int i=0;i<content.word_mean.length;i++){ System.out.println("word_mean"+content.word_mean[i]); } } }
第三步:
封装网络请求接口:HttpRequest_Interface
package com.retrofit.ych.retrofit.http; import com.retrofit.ych.retrofit.entity.Translation; import retrofit2.Call; import retrofit2.http.HTTP; import retrofit2.http.Query; //封装网络请求的接口 public interface HttpRequest_Interface { @HTTP(method = "POST",path = "ajax.php",hasBody = true) Call<Translation> getCall(@Query("a") String fy,@Query("f") String f,@Query("t") String t,@Query("w") String w); }
第四步:
创建网络请求访问类:HttpRequest
package com.retrofit.ych.retrofit.http; import android.util.Log; import com.retrofit.ych.retrofit.entity.Translation; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class HttpRequest { public static void request(){ //创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://fy.iciba.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); //创建网络请求接口的实例 final HttpRequest_Interface request = retrofit.create(HttpRequest_Interface.class); //对发送请求进行封装 Call<Translation> call = request.getCall("fy","auto","auto","hello"); //异步发送网络请求 call.enqueue(new Callback<Translation>() { @Override public void onResponse(Call<Translation> call, Response<Translation> response) { //请求成功,进入 //处理返回的数据 Translation translation = response.body(); translation.show(); } @Override public void onFailure(Call<Translation> call, Throwable t) { //请求失败进入 Log.e("tag","请求失败"); } }); } }
第五步:
入口:MainActivity
package com.retrofit.ych.retrofit; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import static com.retrofit.ych.retrofit.http.HttpRequest.request; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); request(); } }
简单demo,请勿喷,谢谢!
下载:https://download.csdn.net/download/yangchanghong1995/10470447
标签:ble 定义数据 iss appcompat word 发送 import blog interface
原文地址:https://www.cnblogs.com/2484ydv/p/9257783.html