我在Android上将内容发布到Drupal网站时遇到问题 我收到错误消息:
需要节点类型
。
我在Postman测试了网址。它有效。我可以添加新文章。它只是在Android上我有这个错误。
我有一个问题。我是否需要添加会话ID和会话名称才能发帖?如果是这样,请举例说明如何实现这一点。
这是我第一次提出要求。不确定有多少代码是正确的。我很难为初学者找到一个好的教程。
在Manifest中我有
android.permission.INTERNET<br>
android.permission.ACCESS_NETWORK_STATE
我保存了我的令牌,会话ID和会话名称。
public String session_name;
public String session_id;
public String token;
发布值
@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();
}
}
答案 0 :(得分:1)
我使用此代码解决了这个问题。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://yoursite/endpoint/node");
try {
//get title and body UI elements
TextView txtTitle = (TextView) findViewById(R.id.editTitle);
TextView txtBody = (TextView) findViewById(R.id.editBody);
//extract text from UI elements and remove extra spaces
title= txtTitle.getText().toString().trim();
body=txtBody.getText().toString().trim();
// set json to StringEntity
StringEntity se = new StringEntity( " { \"title\":\""+title+"\",\"type\":\"article\",\"body\":{\"und\":[{ \"value\":\""+body+"\"}]}}");
// set httpPost Entity
httppost.setEntity(se);
// Set some headers to inform server about the type of the content
httppost.setHeader("Content-type", "application/json");
httppost.setHeader("Cookie",session_name+"="+session_id);
httppost.setHeader("X-CSRF-Token", session_token);
// 8. Execute POST request to the given URL
HttpResponse response = httpclient.execute(httppost);
Log.v("Response from Drupal: ", EntityUtils.toString(response.getEntity()));
return 0;
}catch (Exception e) {
Log.v("Error adding article",e.getMessage());
}