2017-09-04 60 views
0

TL; DR


新加裝因此它可能是一個小白錯誤。終端在郵差中工作正常,我在響應中獲得Response{protocol=http/1.1, code=200, message=OK, url=http://192.168.1.249/api/v1/user/1},但沒有與用戶相關的數據。改造2 - 獲取200響應,但沒有得到返回的數據

的問題


我已經建立了使用Laravel,我想從設備訪問REST API。將會有許多終端需要被訪問,但我無法讓他們工作。我在加入剩下的部分之前試圖讓其工作,但即使我得到了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" 
     } 
    } 
} 

解決方案


我需要知道我要去的地方錯誤。我是新的改造,所以我確信我實施了錯誤的東西,但我看不到問題。

在此先感謝您的幫助。

+0

用戶回調beign是否被調用?你是否在堆棧跟蹤中發現任何錯誤? 你的清單中是否有互聯網許可? –

+0

它被調用,並沒有我沒有得到任何錯誤 – mtrueblood

+0

你可以把一個「斷點」(https://developer.android.com/studio/debug/index.html#breakPoints)看看發生了什麼或你可以打印日誌,檢查它是否沒有進入空的其他情況。 –

回答

4

我覺得現在的問題是,你有Call<User>,但作爲您的JSON你是從服務器的響應越來越UserResult,所以不是一定要Call<UserResult>和userCallback同樣的,而不是

Callback<User> userCallback = new Callback<User> 

它必須是

Callback<UserResult> userCallback = new Callback<UserResult> 

和相應的相應

public void onResponse(Call<UserResult> call, Response<UserResult> response) { 
      if (response.isSuccessful()) { 
       User data = new User(); 
       data = response.body().user; 
      } else { 

      } 
     } 
1

嘗試這樣做

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    createCdenAPI(); 
    Response<User> response = cdenAPI.getUser(requested_with, content_type, "1").execute(); 

    User user = response.body(); 
} 
+0

我不太熟悉線程來實現這一點。哪個類應該實現可運行? – mtrueblood

+0

如果你正在使用改造,我認爲你不需要使用Runnable。 –

+0

Retrofit爲您處理線程和緩存! –

相關問題