如何在android中使用retrofit调用php?

时间:2016-10-21 12:41:04

标签: php android retrofit

使用function setSignature() { var newSig = Gmail.newSendAs(); newSig.signature = "MY NEW SIGNATURE!" Gmail.Users.Settings.SendAs.patch(newSig, "me", Session.getActiveUser().getEmail()) } 从Android应用调用php(post)。

我在onResponse中获得了php的响应,但是无法接收json参数。

Retrofit

MainActivity.java

Php url :
     http://www.example.com/projectname/login.php 
Parameter :
username = "mumbai"
password = "mumbai"

APIClient.java

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        Call sosSensCall = apiService.getLogin("mumbai", "mumbai");
        sosSensCall.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Example example = (Example) response.body();
                Log.d("sk_log","==="+example.getUserId());   
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                Log.d("sk_log", "Failed! Error = " + t.getMessage());
            }
});

ApiInterface.java

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class ApiClient {

    public static final String BASE_URL = "http://www.example.com/";
    private static Retrofit retrofit = null;
    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();           
        }
        return retrofit;
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题: 错误发生在ApiInterface.java中,已将'@Query'更改为'@Field'

修改后的ApiInterface.java

public interface ApiInterface {
   @POST("projectname/login.php")
Call<Example> getLogin(@Field("username") String username, @Field("password") String password);
}