2016-08-10 10 views
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(); 
     } 
    } 

答えて

1

public String session_name; 
public String session_id; 
public String token; 

投稿。

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()); 
     } 
+0

ありがとうございました。私はこの問題を長い間苦労してきました。 – Kong

関連する問題