从数据库获取数据的Web服务如下:
Smart.php
定义('主机','本地主机'); 定义('用户','根'); 定义('PASS','root'); 定义('DB','螺柱'); $con = mysqli_connect(主机,用户,密码,数据库); $sql = "从学生中选择*"; $结果=数组(); $res = mysqli_query($con,$sql); 在此输入代码 while($row = mysqli_fetch_array($res)){ array_push($结果, 数组('id'=>$row[0], '名称'=>$行[1], “地址”=>$行[2] )); } 回声 json_encode(数组(“结果”=>$结果)); mysqli_close($con); ?>
Android程序如下:
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String BASE_URL = "http://localhost:80/Projecr/";
ApiServices as;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.text);
Button btn=(Button)findViewById(R.id.btn);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
as=retrofit.create(ApiServices.class);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
load();
}
});
}
private void load() {
try {
Call<Example> call = as.List();
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
// int statusCode = response.code();
if(!response.isSuccessful())
{
Log.e("API not success",response.toString()+response.message()+response.errorBody()+response.code());
}
Example e = response.body();
if (e == null) {
Toast.makeText(getApplicationContext(), "Null", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Not Null", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
Log.e("Failed","t.printStackTrace()"+t.getCause()+t.getMessage());
tv.setText("Failure");
}
});
}
catch (Exception e)
{
String TAG="tag";
Log.e(TAG, e.getMessage());
}
}
}
示例.java
public class Example {
@SerializedName("result")
@Expose
private List<Result> result = null;
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
}
结果.java
public class Result {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
ApiServices.java接口
public interface ApiServices {
@GET("Smart.php")
Call<Example> List();
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.saloni.retrofit_sample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:id="@+id/text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="click"
/>
</LinearLayout>
当我运行该程序时,单击按钮时,我得到 toast null 并且 logcat 输出如下:
E/API 未成功:retrofit2.Response@d45de1e 未找到 好的http3.ResponseBody$1@9a0e6ff 404
这意味着响应消息是“Not Found”,响应代码是“404”。
按照建议编辑程序并删除“/”后,它在 logcat 中给出以下错误:
E/失败:t.printStackTrace()java.lang.UnsupportedOperationException:接口无法实例化!接口名称:javax.xml.transform.Result无法调用接口javax.xml.transform.Result的无参数构造函数。向 Gson 注册此类型的 InstanceCreator 可能会解决此问题。
public interface ApiServices {
@GET("/Smart.php")
Call<List<Result>> List();
}
而不是
public interface ApiServices {
@GET("Smart.php")
Call<List<Result>> List();
}
我成功解决了这个错误,没有出现任何进一步的错误。我的 Result.java 类与 javax.xml.transform.Result 类冲突。所以我重命名了 Result.java 类