响应消息 - 找不到响应代码-404在改造时调用api

时间:2017-08-19 10:04:36

标签: android web-services retrofit retrofit2 webservices-client

从数据库获取数据的Web服务如下:

Smart.php


        define('HOST','localhost');
        define('USER','root');
        define('PASS','root');
        define('DB','Stud');

      $con = mysqli_connect(HOST,USER,PASS,DB);

      $sql = "select * from Student";

       $result=array();

      $res = mysqli_query($con,$sql);

        enter code here

      while($row = mysqli_fetch_array($res)){
        array_push($result,
        array('id'=>$row[0],
        'name'=>$row[1],
        'address'=>$row[2]
      ));
    }
    echo json_encode(array("result"=>$result));

    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());
        }
    }
}

Example.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;
    }

}

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未找到   okhttp3.ResponseBody$1@9a0e6ff 404

这意味着响应消息为“Not Found”,响应代码为“404”。

按照建议编辑程序并删除“/”后,它在logcat中出现以下错误:

  

E / Failed:t.printStackTrace()java.lang.UnsupportedOperationException:接口无法实例化!接口名称:javax.xml.transform.ResultUnable,用于为接口javax.xml.transform.Result调用no-args构造函数。使用Gson为此类型注册InstanceCreator可以解决此问题。

2 个答案:

答案 0 :(得分:1)

public interface ApiServices {

@GET("/Smart.php")
Call<List<Result>> List();

}

而不是

public interface ApiServices {

@GET("Smart.php")
Call<List<Result>> List();

}

答案 1 :(得分:0)

我成功解决了这个错误而没有任何进一步的错误。我的Result.java类与javax.xml.transform.Result类冲突。所以我重命名了我的Result.java类