0

I need some help with converting HttpClient to HttpURLConnection. I followed this tutorial online and it was really old. If there is a newer and better way to write some of the code, please let me know.

The tutorial shows you how to use Android to post articles on Drupal. Rest api ect

Thanks in advance.

public String session_id;
public String session_name;

//click listener for addArticle button
public void saveArticleButton_click(View view){
    //initiate the background process to post the article to the Drupal endpoint.
    //pass session_name and session_id
    new addArticleTask().execute(session_name, session_id);
}

//asynchronous task to add the article into Drupal
private class addArticleTask extends AsyncTask<String, Void, Integer> {

    protected Integer doInBackground(String... params) {
        //read session_name and session_id from passed parameters
        String session_name=params[0];
        String session_id=params[1];

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://app.flickgo.com/apistuff/node.json");

        try {
            //get title and body UI elements
            TextView txtTitle = (TextView) findViewById(R.id.editTextTitle);
            TextView txtBody = (TextView) findViewById(R.id.editTextBox);

            //extract text from UI elements and remove extra spaces
            String title = txtTitle.getText().toString().trim();
            String body = txtBody.getText().toString().trim();

            //add raw json to be sent along with the HTTP POST request
            StringEntity se = new StringEntity( " { \"title\":\""+title+"\",\"type\":\"article\",\"body\":{\"und\":[{ \"value\":\""+body+"\"}]}}");
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httppost.setEntity(se);

            BasicHttpContext mHttpContext = new BasicHttpContext();
            CookieStore mCookieStore = new BasicCookieStore();

            //create the session cookie
            BasicClientCookie cookie = new BasicClientCookie(session_name, session_id);
            cookie.setVersion(0);
            cookie.setDomain("app.flickgo.com");
            cookie.setPath("/");
            mCookieStore.addCookie(cookie);
            cookie = new BasicClientCookie("has_js", "1");
            mCookieStore.addCookie(cookie);
            mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

            httpclient.execute(httppost,mHttpContext);

            return 0;

        }catch (Exception e) {
            Log.v("Error adding article",e.getMessage());
        }
        return 0;
    }

    protected void onPostExecute(Integer result) {
        //start the List Activity and pass back the session_id and session_name
        Intent intent = new Intent(AddArticle.this, ListActivity.class);
        intent.putExtra("SESSION_ID", session_id);
        intent.putExtra("SESSION_NAME", session_name);
        startActivity(intent);

        //stop the current activity
        finish();
    }
}
}
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Kong
  • 315
  • 2
  • 12

1 Answers1

1

HttpURLConnection has an awkward API. You might try OK HTTP instead. Regardless, I think the HttpURLConnection code looks like:

CookieManager cookies = new CookieManager();
cookies.getCookieStore().add(null, new HttpCookie("app.flickgo.com", "/"));
cookies.getCookieStore().add(null, new HttpCookie("has_js", "1"));
cookies.getCookieStore().add(null, new HttpCookie(session_name, session_id));

URL url = new URL("http://app.flickgo.com/apistuff/node.json");
HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection();
urlconnection.setRequestMethod("POST");
urlconnection.setDoOutput(true);
urlconnection.setRequestProperty("Content-Type", "application/json");

try(OutputStreamWriter writer = new OutputStreamWriter(urlconnection.getOutputStream())) {
    writer.write("{\"title\":\""+title+"\",\"type\":\"article\",\"body\":{\"und\":[{ \"value\":\""+body+"\"}]}}");
}

int rc = urlconnection.getResponseCode();
jdgilday
  • 866
  • 7
  • 21