2016-10-21 20 views
-3

Retrofitを使ってアンドロイドアプリからphp(post)を呼び出す。アンドロイドで改造を使用して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); 
} 
+0

あなたの問題は何ですか? –

+0

JSONから値を読み取ることができませんでした。 –

答えて

-1

ApiInterface.java: エラーがApiInterface.javaにあったが、変更されている '@query' '@Field'

修正ApiInterfaceへ.java

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