2016-09-14 71 views
0

無法發送的崗位API數據原始JSON數據使用Retrofit 2.0無法使用改造2.0

try { 
    JSONObject myUserData = new JSONObject(); 
    myUserData.put("mobile", "917023847899"); 
    myUserData.put("displayName", "Deepuu"); 
    myUserData.put("info", "AD"); 
    myUserData.put("photo", ""); 
    myUserData.put("displayName", "Deepu"); 

    JSONObject deviceData = new JSONObject(); 
    ; 
    deviceData.put("deviceId", "2124578910556991"); 
    deviceData.put("pnToken", "klklklkl"); 
    deviceData.put("os", "android"); 
    deviceData.put("targetTopic", "jkjkjkjkj"); 

    myUserData.put("device", "" + deviceData); 
    OkHttpClient client = new OkHttpClient(); 

    Retrofit retrofit = new Retrofit.Builder() 
        .baseUrl(URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 

    Api Ainterface = retrofit.create(Api.class); 

    Call<Response> data = Ainterface.getResponse(myUserData); 
    data.enqueue(new Callback<Response>() { 
    @Override 
    public void onResponse(Call<Response> call, retrofit2.Response<Response> response) { 
     if (response.isSuccessful()) { 
     Response rr = response.body(); 
     Integer ss = rr.getStatusCode(); 
     Log.e("I Am", "Success" ); 
     } 
    } 

    @Override 
    public void onFailure(Call<Response> call, Throwable t) { 
     Log.e("I Am", "Failed"); 
    } 
    }); 

MyInterface的在POST API數據發送原始JSON數據:

@POST("appUsers/") 
Call<Response> getResponse(@Body RequestBody value); 

任何人都可以提出一個解決方案使用Retrofit 2.0發送原始JSON數據POST api?我新使用Retrofit;請在onResponse方法中獲得null響應。

+1

添加您的API和改造服務生成器類代碼太 – USKMobility

+0

問題是我不能夠發送這些JSON對象perameters並得到空應答.. :( –

回答

1

您正在使用GsonConverterFactory。所以,你應該將通過改進轉換的模型類對象傳遞給字符串並添加到請求體中。但在你的代碼中,你通過了JSONObject。

創建模型類,如下所示,並根據您的項目結構變化軟件包名稱:

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class Device { 

@SerializedName("deviceId") 
@Expose 
private String deviceId; 
@SerializedName("pnToken") 
@Expose 
private String pnToken; 
@SerializedName("os") 
@Expose 
private String os; 
@SerializedName("targetTopic") 
@Expose 
private String targetTopic; 

/** 
* 
* @return 
* The deviceId 
*/ 
public String getDeviceId() { 
return deviceId; 
} 

/** 
* 
* @param deviceId 
* The deviceId 
*/ 
public void setDeviceId(String deviceId) { 
this.deviceId = deviceId; 
} 

/** 
* 
* @return 
* The pnToken 
*/ 
public String getPnToken() { 
return pnToken; 
} 

/** 
* 
* @param pnToken 
* The pnToken 
*/ 
public void setPnToken(String pnToken) { 
this.pnToken = pnToken; 
} 

/** 
* 
* @return 
* The os 
*/ 
public String getOs() { 
return os; 
} 

/** 
* 
* @param os 
* The os 
*/ 
public void setOs(String os) { 
this.os = os; 
} 

/** 
* 
* @return 
* The targetTopic 
*/ 
public String getTargetTopic() { 
return targetTopic; 
} 

/** 
* 
* @param targetTopic 
* The targetTopic 
*/ 
public void setTargetTopic(String targetTopic) { 
this.targetTopic = targetTopic; 
} 

} 
-----------------------------------com.example.Request.java----------------------------------- 

package com.example; 

import javax.annotation.Generated; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

@Generated("org.jsonschema2pojo") 
public class Request { 

@SerializedName("mobile") 
@Expose 
private String mobile; 
@SerializedName("info") 
@Expose 
private String info; 
@SerializedName("photo") 
@Expose 
private String photo; 
@SerializedName("displayName") 
@Expose 
private String displayName; 
@SerializedName("device") 
@Expose 
private Device device; 

/** 
* 
* @return 
* The mobile 
*/ 
public String getMobile() { 
return mobile; 
} 

/** 
* 
* @param mobile 
* The mobile 
*/ 
public void setMobile(String mobile) { 
this.mobile = mobile; 
} 

/** 
* 
* @return 
* The info 
*/ 
public String getInfo() { 
return info; 
} 

/** 
* 
* @param info 
* The info 
*/ 
public void setInfo(String info) { 
this.info = info; 
} 

/** 
* 
* @return 
* The photo 
*/ 
public String getPhoto() { 
return photo; 
} 

/** 
* 
* @param photo 
* The photo 
*/ 
public void setPhoto(String photo) { 
this.photo = photo; 
} 

/** 
* 
* @return 
* The displayName 
*/ 
public String getDisplayName() { 
return displayName; 
} 

/** 
* 
* @param displayName 
* The displayName 
*/ 
public void setDisplayName(String displayName) { 
this.displayName = displayName; 
} 

/** 
* 
* @return 
* The device 
*/ 
public Device getDevice() { 
return device; 
} 

/** 
* 
* @param device 
* The device 
*/ 
public void setDevice(Device device) { 
this.device = device; 
} 

} 

和POST請求如下:根據您的請求和響應模型

@POST("yourpath") 
    Call<YourResponseModelClass> getResponse(@Body RequestModelClass request); 

更新YourResponseModelClass & RequestModelClass類。

您可以生成從JSON模型類,http://www.jsonschema2pojo.org/

+0

我不理解.my api需要將參數作爲「原始」數據傳遞,並且我必須在該函數中傳遞一個json對象。我唯一想知道的是我該怎麼做,如果我通過了一個模型類,作爲json對象傳遞 –

+0

謝謝@USKMobility我通過使用你的方法得到了我的迴應 –

-1

您可以在復古2.0後生的JSONObject。需要將其轉換成RequestBody這樣

RequestBody requestBody =RequestBody.create(MediaType.parse("application/json"),myUserData.toString()); 
Call<Response> data = Ainterface.getResponse(requestBody); 
+0

不工作.. !! –

+0

@POST(「appUsers /」)。slash after'appuser' ????? –

+0

沒有影響添加和刪​​除這個斜槓。 –