-3
는 개조를 사용하여 안드로이드 응용 프로그램에서 PHP (포스트)를 호출합니다.안드로이드에서 retrofit을 사용하여 PHP를 호출하는 방법은 무엇입니까?
내가 성공으로 onResponse에 PHP에서 응답을하지만,없는이 JSON 매개 변수를받을 수 있습니다.
PHP는 URL : http://www.example.com/projectname/login.php 매개 변수 : 사용자 이름 = "뭄바이" 암호 = "뭄바이"
MainActivity.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());
}
});
APIClient.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;
}
}
가 나는 버그가 해결되었다
public interface ApiInterface {
@POST("projectname/login.php")
Call<Example> getLogin(@Query("username") String username, @Query("password") String password);
}
문제가 정확히 무엇입니까? –
JSON에서 값을 읽을 수 없습니다. –