1
AndroidからDrupalサイトにコンテンツを投稿する際に問題があります。Android HttpURLConnection、POST:エラーメッセージ、ノードタイプが必要です
ノードタイプは
必要です:
私はエラーメッセージが表示されます。
私は郵便配達所でURLをテストしました。そして、それは動作します。私は新しい記事を追加することができます。 Android上でこのエラーが発生したばかりです。
私は質問があります。投稿を行うには、セッションIDとセッション名を追加する必要がありますか?もしそうなら、これがどのように達成されたかの例を挙げてください。
これは初めての投稿リクエストです。どのくらいのコードが正しいかわからない。私は初心者のための良いチュートリアルを見つけるのが難しかった。マニフェストに
私はトークン、セッションIDおよびセッション名を保存した
android.permission.INTERNET<br>
android.permission.ACCESS_NETWORK_STATE
を持っています。私はこのコードを使用することによって、問題を解決するための値
@Override
protected String doInBackground(Void... voids) {
String address = "http://app.flickgo.com/apistuff/node.json";
HttpURLConnection urlConnection;
try {
URL url = new URL(address);
urlConnection = (HttpURLConnection) url.openConnection();
String userCredentials = "My_Username:My_Password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
urlConnection.setRequestProperty ("Authorization", basicAuth);
urlConnection.setRequestProperty("X-CSRF-Token", token);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
//i want to save the title of an article
String title = "Mobile";
String input = ("{\"title\":\""+title+"\",\"type\":\"article\"}");
writer.write(input);
writer.flush();
writer.close();
outputStream.close();
JSONObject jsonObject = new JSONObject();
InputStream inputStream;
// get stream
if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
}
// parse stream
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp, response = "";
while ((temp = bufferedReader.readLine()) != null) {
response += temp;
}
// put into JSONObject
jsonObject.put("Content", response);
jsonObject.put("Message", urlConnection.getResponseMessage());
jsonObject.put("Length", urlConnection.getContentLength());
jsonObject.put("Type", urlConnection.getContentType());
return jsonObject.toString();
} catch (IOException | JSONException e) {
return e.toString();
}
}
ありがとうございました。私はこの問題を長い間苦労してきました。 – Kong