❔ Java => C#
Hi all,
Would anyone know enough Java to convert this codeblock from Java to C#?
ChatGPT failed on this convert lol
8 Replies
1.
private final String licenseKey;
-> private readonly string licenseKey;
2. String
-> string
.
3. OkHttpClient
-> I think this would just be HttpClient
.
4. Request request = new Request.Builder()
-> Something along the lines of a HttpWebRequest
. HttpClient supports build in ways but you add headers and stuff so doing it with HttpWebRequest
is easier.
5. JSONObject obj = new JSONObject(data);
-> Newtonsoft has JObject.Parse()
for parsing json without an explicit type. Recommend you make an explicit type and use JsonConvert.DezerializeObject<Type>()
though.
Instead of Newtonsoft you can also use System.Text.Json btw. Idk the direct syntax for that but it's build into newer .NET versions.1. it's not better to make that a property, that's correct as a field
3. I don't see any async code, not sure if Java even has that. That might need adjusting
5.
JsonSerializer.Deserialize<T>()
3. Remove all this behaviour from the constructor and create a
static async
method that creates a License
class. Then you can call the HttpClient
asynchronously.Use raw string literals and interpolation for that json
Oh wait yeah, that's all in the ctor
That's too much code to be there
not sure if Java even has that.there is no
async
and await
in java sadly. But there is the Future Api with a CompletableFuture
type. Thats what comes closest to our loved Task
most of that shouldnt be in the ctor
I think we established that 😄
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.