Add categories to post request
This commit is contained in:
parent
40091c4e20
commit
60678b151c
|
@ -31,11 +31,14 @@ dependencies {
|
|||
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
|
||||
implementation 'com.android.support:design:26.1.0'
|
||||
implementation 'com.squareup.okhttp3:okhttp:3.9.1'
|
||||
// Logging
|
||||
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
|
||||
|
||||
// ViewModel and LiveData
|
||||
implementation "android.arch.lifecycle:extensions:1.1.0"
|
||||
annotationProcessor "android.arch.lifecycle:compiler:1.1.0"
|
||||
|
||||
|
||||
// Java8 support for Lifecycles
|
||||
implementation "android.arch.lifecycle:common-java8:1.1.0"
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ import okhttp3.OkHttpClient;
|
|||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
|
||||
public class PostMessageTask extends AsyncTask<String, Void, String> {
|
||||
private final String accessToken;
|
||||
|
@ -31,17 +32,29 @@ public class PostMessageTask extends AsyncTask<String, Void, String> {
|
|||
|
||||
@Override
|
||||
protected String doInBackground(String... strings) {
|
||||
FormBody.Builder builder = new FormBody.Builder();
|
||||
builder.add("content", postModel.content.get());
|
||||
RequestBody formBody = builder.build();
|
||||
String content = postModel.content.get();
|
||||
|
||||
FormBody.Builder builder = new FormBody.Builder();
|
||||
builder.add("h", "entry")
|
||||
.add("content", content);
|
||||
|
||||
addCategories(builder, postModel.category.get());
|
||||
|
||||
RequestBody formBody = builder.build();
|
||||
Request request = new Request.Builder()
|
||||
.addHeader("Authorization", "Bearer " + accessToken)
|
||||
.method("POST", formBody)
|
||||
.url(micropubBackend)
|
||||
.build();
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
|
||||
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
|
||||
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.addInterceptor(logging)
|
||||
.build();
|
||||
|
||||
|
||||
String msg;
|
||||
Call call = client.newCall(request);
|
||||
Response response = null;
|
||||
|
@ -59,8 +72,16 @@ public class PostMessageTask extends AsyncTask<String, Void, String> {
|
|||
}
|
||||
}
|
||||
|
||||
private FormBody.Builder addCategories(FormBody.Builder builder, String category) {
|
||||
String[] categories = category.split("\\s+");
|
||||
for (String cat : categories) {
|
||||
builder.add("category[]", cat);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
protected void onPostExecute(String message) {
|
||||
Toast.makeText(context.get(), message, Toast.LENGTH_SHORT).show();
|
||||
postModel.content.set("");
|
||||
postModel.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,15 @@ import android.databinding.ObservableField;
|
|||
|
||||
public class PostViewModel extends ViewModel {
|
||||
public final ObservableField<String> content = new ObservableField<>();
|
||||
public final ObservableField<String> category = new ObservableField<>();
|
||||
|
||||
public PostViewModel() {
|
||||
this.content.set("test");
|
||||
this.content.set("");
|
||||
this.category.set("");
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.content.set("");
|
||||
this.category.set("");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
android:background="@android:color/white"
|
||||
android:ems="10"
|
||||
android:gravity="top"
|
||||
android:hint="Content"
|
||||
android:inputType="textMultiLine"
|
||||
android:padding="3dp"
|
||||
android:singleLine="false"
|
||||
|
@ -40,12 +41,11 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:onClick="sendPost"
|
||||
android:text="Post"
|
||||
app:layout_constraintEnd_toEndOf="@+id/content"
|
||||
app:layout_constraintTop_toBottomOf="@+id/content" />
|
||||
app:layout_constraintEnd_toEndOf="@+id/editCategory"
|
||||
app:layout_constraintTop_toBottomOf="@+id/editCategory" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button"
|
||||
|
@ -68,19 +68,22 @@
|
|||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView2"
|
||||
<EditText
|
||||
android:id="@+id/editCategory"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_height="36dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:text="@{viewModel.content}"
|
||||
app:layout_constraintBottom_toTopOf="@+id/button"
|
||||
android:background="@android:color/background_light"
|
||||
android:ems="10"
|
||||
android:hint="Categories (space separated)"
|
||||
android:inputType="text"
|
||||
android:singleLine="true"
|
||||
android:text="@={viewModel.category}"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/btnPost" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/content" />
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user