TL返さ;
新しいDRレトロフィット2から200応答の取得ではなくなったデータは、
それはおそらくnoobのミスですので、改造します。エンドポイントは郵便配達でうまく動作し、応答にはResponse{protocol=http/1.1, code=200, message=OK, url=http://192.168.1.249/api/v1/user/1}
が得られますが、ユーザーに関連するデータはありません。
問題
私は、デバイスからアクセスしたいというLaravelを使用してREST APIを構築しています。アクセスする必要があるエンドポイントは数多くありますが、そのうちのいずれかを動作させることはできません。私は残りの部分を追加する前に1つを稼働させようとしていますが、200が得られたとしてもデータは得られません。
すべてのプロパティはresponse.bodyにありますが、すべてnullです。
コード
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.x.tools.actualrest.rest.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends Activity {
private CdenApi cdenAPI;
private String token;
String requested_with = "XMLHttpRequest";
String content_type = "application/json";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createCdenAPI();
cdenAPI.getUser(requested_with, content_type, "1").enqueue(userCallback);
}
@Override
protected void onResume() {
super.onResume();
}
private void createCdenAPI() {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(CdenApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
cdenAPI = retrofit.create(CdenApi.class);
}
Callback<User> userCallback = new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
User data = new User();
data = response.body();
} else {
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
t.printStackTrace();
}
};
}
CdenApi.java
import com.x.tools.actualrest.rest.User;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Path;
import retrofit2.Call;
public interface CdenApi {
String BASE_URL = "http://192.168.1.249/api/v1/";
@GET("user/{id}")
Call<User> getUser(@Header("X-Requested-With") String req_with, @Header("Content-Type") String cont_type, @Path("id")String userId);
}
User.java
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("id")
public Integer id;
@SerializedName("name")
public String name;
@SerializedName("email")
public String email;
@SerializedName("access_level")
public Integer accessLevel;
@SerializedName("created_at")
public String createdAt;
@SerializedName("updated_at")
public String updatedAt;
@SerializedName("view_boxes")
public ViewBoxes viewBoxes;
}
UserResult.java
import com.google.gson.annotations.SerializedName;
public class UserResult {
@SerializedName("msg")
public String msg;
@SerializedName("user")
public User user;
}
ViewBoxes.java
import com.google.gson.annotations.SerializedName;
public class ViewBoxes {
@SerializedName("href")
public String href;
@SerializedName("method")
public String method;
}
エンドポイントからのJSON:
{
"msg": "User information",
"user": {
"id": 1,
"name": "Guy",
"email": "[email protected]",
"access_level": 1,
"created_at": "2017-08-22 15:53:06",
"updated_at": "2017-08-22 15:53:06",
"view_boxes": {
"href": "api/v1/user/1",
"method": "GET"
}
}
}
ソリューション
私はつもりです場所を知る必要がある
違う。私は改造のために新しいので、私は間違った何かを実装していると確信していますが、私は問題を見ることができません。
ご協力いただきありがとうございます。
ユーザーコールバックはコールされていますか?スタックトレースに何かエラーがありますか? マニフェストにインターネットアクセス権がありますか? –
呼び出されていて、何もエラーが発生していない – mtrueblood
「ブレークポイント」(https://developer.android.com/studio/debug/index.html#breakPoints)を使用して、何が起こっているのかを知ることができます。ログを出力し、それが空のelse条件に行かないことを確認することができます。 –