From 30c45574a65556442f0f30418063a29f9764d092 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 25 Feb 2018 15:20:21 +0100 Subject: [PATCH] Improvement of post creation --- .../eu/stuifzand/micropub/MainActivity.java | 12 +- .../main/java/eu/stuifzand/micropub/Post.java | 23 ---- .../eu/stuifzand/micropub/client/Client.java | 106 ++++++++++++++++++ .../eu/stuifzand/micropub/client/Post.java | 56 +++++++++ .../stuifzand/micropub/client/Response.java | 27 +++++ 5 files changed, 199 insertions(+), 25 deletions(-) delete mode 100644 app/src/main/java/eu/stuifzand/micropub/Post.java create mode 100644 app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Client.java create mode 100644 app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Post.java create mode 100644 app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Response.java diff --git a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java index 3d6ff98..9d0f6bb 100644 --- a/app/src/main/java/eu/stuifzand/micropub/MainActivity.java +++ b/app/src/main/java/eu/stuifzand/micropub/MainActivity.java @@ -24,6 +24,8 @@ import android.widget.Toast; import java.io.IOException; import eu.stuifzand.micropub.databinding.ActivityMainBinding; +import eu.stuifzand.micropub.eu.stuifzand.micropub.client.Client; +import eu.stuifzand.micropub.eu.stuifzand.micropub.client.Post; import okhttp3.HttpUrl; public class MainActivity extends AppCompatActivity { @@ -132,8 +134,14 @@ public class MainActivity extends AppCompatActivity { if (micropubBackend != null) { Log.i("micropub", "Sending message to " + micropubBackend); - new PostMessageTask(MainActivity.this, token, model, micropubBackend) - .execute(); + Client client = new Client(getApplication()); + client.getResponse().observe(MainActivity.this, response -> { + Log.i("micropub", "response received " + response.isSuccess()); + if (response.isSuccess()) { + model.clear(); + } + }); + client.createPost(new Post(null, model.content.get(), model.category.get(), HttpUrl.parse(model.inReplyTo.get())), token, HttpUrl.parse(micropubBackend)); } } diff --git a/app/src/main/java/eu/stuifzand/micropub/Post.java b/app/src/main/java/eu/stuifzand/micropub/Post.java deleted file mode 100644 index 94345fd..0000000 --- a/app/src/main/java/eu/stuifzand/micropub/Post.java +++ /dev/null @@ -1,23 +0,0 @@ -package eu.stuifzand.micropub; - -import okhttp3.FormBody; - -public class Post { - private String content; - - public Post(String content) { - this.content = content; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public void applyToFormBodyBuilder(FormBody.Builder builder) { - builder.add("content", content); - } -} diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Client.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Client.java new file mode 100644 index 0000000..78d48d3 --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Client.java @@ -0,0 +1,106 @@ +package eu.stuifzand.micropub.eu.stuifzand.micropub.client; + +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.MutableLiveData; +import android.os.AsyncTask; +import android.support.annotation.NonNull; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import okhttp3.Call; +import okhttp3.FormBody; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.logging.HttpLoggingInterceptor; + +public class Client extends AndroidViewModel { + private MutableLiveData response = new MutableLiveData<>(); + + public Client(@NonNull Application application) { + super(application); + } + + public void createPost(Post post, String accessToken, HttpUrl micropubBackend) { + new PostTask(post, micropubBackend, accessToken, response).execute(); + } + + public LiveData getResponse() { + return response; + } + + private static class PostTask extends AsyncTask { + + private Post post; + private HttpUrl micropubBackend; + private String accessToken; + private MutableLiveData response; + + PostTask(Post post, HttpUrl micropubBackend, String accessToken, MutableLiveData response) { + this.post = post; + this.micropubBackend = micropubBackend; + this.accessToken = accessToken; + this.response = response; + } + + @Override + protected Void doInBackground(String... strings) { + FormBody.Builder builder = new FormBody.Builder(); + builder.add("h", "entry") + .add("content", post.getContent()); + + for (String cat : post.getCategories()) { + builder.add("category[]", cat); + } + + if (post.hasInReplyTo()) { + builder.add("in-reply-to", post.getInReplyTo()); + } + + if (post.hasName()) { + builder.add("name", post.getName()); + } + + RequestBody formBody = builder.build(); + micropubBackend = HttpUrl.parse("http://192.168.178.21:5000/micropub"); + Request request = new Request.Builder() + .addHeader("Authorization", "Bearer " + accessToken) + .method("POST", formBody) + .url(micropubBackend) + .build(); + + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); + logging.setLevel(HttpLoggingInterceptor.Level.BODY); + + OkHttpClient client = new OkHttpClient.Builder() + .addInterceptor(logging) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .build(); + + Call call = client.newCall(request); + okhttp3.Response httpResponse = null; + try { + httpResponse = call.execute(); + if (httpResponse.code() == 201) { + String location = httpResponse.header("Location"); + response.postValue(Response.successful(location)); + } else { + response.postValue(Response.failed()); + } + } catch (IOException e) { + response.postValue(Response.failed()); + } finally { + if (httpResponse != null) { + httpResponse.close(); + } + } + return null; + } + } +} diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Post.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Post.java new file mode 100644 index 0000000..cd7f89a --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Post.java @@ -0,0 +1,56 @@ +package eu.stuifzand.micropub.eu.stuifzand.micropub.client; + +import okhttp3.HttpUrl; + +public class Post { + private String name; + private String content; + private String[] categories; + private HttpUrl inReplyTo; + + public Post(String content) { + this.content = content; + this.categories = new String[]{}; + } + + public Post(String name, String content) { + this.name = name; + this.content = content; + this.categories = new String[]{}; + } + + public Post(String name, String content, String categories) { + this.name = name; + this.content = content; + this.categories = categories.split("\\s+"); + } + + public Post(String name, String content, String categories, HttpUrl inReplyTo) { + this(name, content, categories); + this.inReplyTo = inReplyTo; + } + + + public String getContent() { + return content; + } + + public boolean hasName() { + return this.name != null && !name.equals(""); + } + public String getName() { + return this.name; + } + + public boolean hasInReplyTo() { + return inReplyTo != null; + } + + public String getInReplyTo() { + return inReplyTo.toString(); + } + + public String[] getCategories() { + return categories; + } +} diff --git a/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Response.java b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Response.java new file mode 100644 index 0000000..90c6ec4 --- /dev/null +++ b/app/src/main/java/eu/stuifzand/micropub/eu/stuifzand/micropub/client/Response.java @@ -0,0 +1,27 @@ +package eu.stuifzand.micropub.eu.stuifzand.micropub.client; + +public class Response { + private boolean success; + private String url; + + protected Response(boolean success) { + this.success = success; + } + + protected Response(boolean success, String url) { + this.success = success; + this.url = url; + } + + public static Response failed() { + return new Response(false); + } + + public static Response successful(String url) { + return new Response(true, url); + } + + public boolean isSuccess() { + return success; + } +}