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