DataAPI wants to post articles on Android! It is a very important API at that time. This time, I would like to briefly explain the specification of categories from among them.
String cgi_url = "http://path/to/mt-data-api.cgi;
String blog_id = "id";
String username = "username"
String password = "password"
String token;
ArrayList<String> categoryLabel;
ArrayList<Integer> categoryId;
AsyncHttpClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
categoryLabel = new ArrayList<String>();
categoryId = new ArrayList<Integer>();
client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("username", username);
params.put("password", password);
params.put("clientId", "test");
String url = cgi_url.concat("/v2/authentication");
client.post(url, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject res) {
try {
token = res.getString("accessToken");
Toast.makeText(act, "You are now logged", Toast.LENGTH_LONG).show();
preferencesUtil.putInt("InitState", 1);
} catch (Exception e) {
Toast.makeText(act, "Login error", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Throwable e, JSONObject res) {
Toast.makeText(act, "Login error", Toast.LENGTH_LONG).show();
}
});
url = cgi_url.concat("/v2/sites/2/categories");
client.get(url, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject json) {
try {
//To get the title
JSONArray datas = json.getJSONArray("items");
for(int i=0; i<datas.length(); i++) {
categoryLabel.add(datas.getJSONObject(i).getString("label"));
categoryId.add(Integer.parseInt(datas.getJSONObject(i).getString("id")));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable e, JSONObject res) {
Toast.makeText(act, "Login error", Toast.LENGTH_LONG).show();
}
});
}
Declare cgi_url: DataAPI endpoint
, blog_id: blog ID
, ʻusername: username,
password: password` in the member area.
Get articles in ʻonCreate`.
To log in, insert the member variables ʻusername and
passwordinto
RequestParams and ** post ** to
cgi_url + / v2 / authentication with ʻAsyncHttpClient
.
If you log in successfully, you will enter ʻonSuccess and
JSONObject will be returned, so save ʻaccessToken
.
To get the article category, ** post ** the RequestParams
created earlier to cgi_url + / v2 / sites / 2 / categories
with ʻAsyncHttpClient`.
If the category is successfully acquired, it will enter ʻonSuccess, so insert the element of ʻitems
into JSONArray
.
JSONArray datas = json.getJSONArray("items");
Get the label
and ʻid of the registered category from the obtained ʻitems
.
for(int i=0; i<datas.length(); i++) {
categoryLabel.add(datas.getJSONObject(i).getString("label"));
categoryId.add(Integer.parseInt(datas.getJSONObject(i).getString("id")));
}
Now you can get the article category.
If you can do this, all you have to do is post.
private void uploadText() throws JSONException {
CharSequence title = "title";
CharSequence body = "body";
JSONObject entry = new JSONObject();
RequestParams params = new RequestParams();
JSONArray categories = new JSONArray();
JSONObject category;
for(int i=0; i<categoryLabel.size(); i++) {
category = new JSONObject();
category.put("id",categoryId.get(i));
categories.put(category);
}
try {
entry.put("title", title);
entry.put("body", body);
entry.put("categories",categories);
entry.put("status", "Draft");
} catch (JSONException e) {
e.printStackTrace();
}
params.put("entry", entry.toString());
String url = cgi_url.concat("/v2/sites/").concat(blog_id).concat("/entries");
Header[] headers = new Header[1];
headers[0] = new BasicHeader("X-MT-Authorization", "MTAuth accessToken=".concat(token));
client.post(getBaseContext(), url, headers, params, "application/x-www-form-urlencoded", new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject res) {
Toast.makeText(act, "send completely", Toast.LENGTH_LONG).show();
}
public void onFailure(Throwable e, JSONObject res) {
Toast.makeText(act, "sending error", Toast.LENGTH_LONG).show();
}
});
}
First, create the entry element JSONObject
and RequestParams
to send the article request.
After that, prepare JSONArray
for inserting multiple categories and JSONObject
for inserting individual categories.
The category entry is id and the category group entry is categories.
When ** post **, send by inserting category
inside categories
.
After that, if you enter the endpoint of ʻurland
headers set as
params with ʻAsyncHttpClient
and send it, you can create an article with the category registered.
Since it uses the internet, it's in Android Manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Don't forget to declare!
Recommended Posts