Jsonとして送信されるクラスを表すWebApiReturnというクラスがあります。
public class WebApiReturn<T> {
@SerializedName("objectReturn")
public T ObjectReturn;
@SerializedName("hasError")
public boolean HasError;
@SerializedName("errorMessage")
public String ErrorMessage;
@SerializedName("errorCode")
public String ErrorCode;
}
それに加えて、Webサービスから取得しようとしている表現クラスがあります。
public class MyObject {
public int ID_Obj;
public String ObjectName;
public Date LastLoginDate;
}
そして、私のWebServiceによって送信されたJsonとしてフォーマットされた文字列を取得し、それをこのJavaクラスに変換するgetObjectという関数:
public Object getObject(Class wantedResponseClass) throws JSONException{
Gson gson = new Gson();
object = gson.fromJson(this.result, wantedResponseClass);
return object;
}
そして、私のJsonStringは次のようになります。
{"objectReturn":{"iD_Obj":123,"objectName":"TestName","lastLoginDate":"0001-01-01T00:00:00"},"hasError":false,"errorMessage":null,"errorCode":null}
私のコードでは、次のようにオブジェクトを取得しようとしています。
WebApiReturn<MyObject> responseFromServer = new WebApiReturn<>();
try {
responseFromServer =(WebApiReturn<MyObject>) getObject(responseFromServer.getClass());
} catch (Exception e) {
e.printStackTrace();
}
しかし、宣言したときにMyObjectに変換される必要がある汎用T WebApiReturn<MyObject> responseFromServer = new WebApiReturn<>();
ObjectReturnは、Jsonからの表現で満たされていません。誰かが私が今何を間違っているのか知っていますか?別のタイプの逆シリアル化などを使用する必要がありますか?
デシリアライザーにジェネリッククラスを含むクラスを作成するには、AsyncTaskを拡張するTのインスタンスを使用してクラスを作成し(UIスレッドの非同期サーバーからJsonを取得するため)、JSONObject AND GsonLibraryを使用してクラスを2つに分割しましたパーツ:クラス自体と内部のジェネリック。次に、2つの要素として戻ります。
これが私のクラスです:
public class RestClient<t> extends AsyncTask<String, String, String> {
private Class<t> tClass;
private t returnGenericObject;
private WebApiReturn webApiReturn;
private AsyncCallback callback;
public RestClient(Class<t> tClass)
{
//The instance of the class you want
this.tClass = tClass;
webApiReturn= new WebApiReturn();
}
//You get the WebApiReturn with this method
public WebApiReturn getWebApiReturn(){
return webApiReturn;
}
//You get the inside generic object with this method
public Object getObjectResponse(){
return returnGenericObject;
}
@Override
protected String doInBackground(String... OnlyTheURL) {
String urlString = OnlyTheURL[0];
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
String result = convertStreamToString(connection.getInputStream()); //A function to convert Stream to String...
connection.disconnect();
//Created a JSONObject from the result of the connection
JSONObject jsonObject = new JSONObject(result);
//Don't set the Generic Object because it will crash when you try to get back
webApiReturn.setHasError(jsonObject.getBoolean("hasError"));
webApiReturn.setErrorMessage (jsonObject.getString("errorMessage"));
webApiReturn.setErrorCode(jsonObject.getString("errorCode"));
//Get the String of the generic object within the WebApiReturn
String obj = jsonObject.get("objetoRetorno").toString();
//With Gson convert it to the class you expect (that you instanciated in the constructor)
Gson gson = new Gson();
ObjetoDeRetorno = gson.fromJson(obj, tClass);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
そして、あなたがそれを呼びたいところ、あなたはちょうどこのようにします:
RestClient<YourObject> restClient = new RestClient<>(YourObject.class);
restClient.execute();
Thread.sleep(5000); //You can do this other better ways, but just as example I'm doing this...
//After the execution you get your objects:
WebApiReturn return = getWebApiReturn();
YourObject object = (YourObject) restClient.getObjectResponse();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加