Improvement of post creation
This commit is contained in:
parent
e0456bc497
commit
30c45574a6
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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> 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<Response> getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
private static class PostTask extends AsyncTask<String, Void, Void> {
|
||||
|
||||
private Post post;
|
||||
private HttpUrl micropubBackend;
|
||||
private String accessToken;
|
||||
private MutableLiveData<Response> response;
|
||||
|
||||
PostTask(Post post, HttpUrl micropubBackend, String accessToken, MutableLiveData<Response> 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user