【问题标题】:Need help converting HttpClient to HttpURLConnection需要帮助将 HttpClient 转换为 HttpURLConnection
【发布时间】:2016-12-17 03:48:37
【问题描述】:

我需要一些帮助来将 HttpClient 转换为 HttpURLConnection。我在网上遵循了这个教程,它真的很老了。如果有更新更好的方法来编写一些代码,请告诉我。

本教程向您展示如何使用 Android 在 Drupal 上发布文章。休息接口

提前致谢。

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

【问题讨论】:

    标签: java android post httpurlconnection


    【解决方案1】:

    HttpURLConnection 有一个笨拙的 API。你可以试试OK HTTP。无论如何,我认为 HttpURLConnection 代码如下所示:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-24
      • 2019-12-23
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多