Search code examples
javaandroidapacheandroidhttpclient

How to HTTPPut in android


Im trying to do a HTTPPut but seems that I cannot instantiate the HTTPPut object. Im refering to this question as a source code, but seems to create a HTTPPut object I need a URI not a URL? Can someone tell me how can I instantiate the HTTPPut object?

Source:

URL url;
                        try {
                            url = new URL(Constants.API_URL + "api/documents/"
                                    + mustRead.getContract_id()
                                    + "/mark_as_read");

                            HttpClient client = new DefaultHttpClient();
                            HttpPut put = new HttpPut(url);

                            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                            pairs.add(new BasicNameValuePair("contract_id",
                                    mustRead.getContract_id() + ""));
                            pairs.add(new BasicNameValuePair("time", URLEncoder
                                    .encode(currentDateandTime)));
                            put.setEntity(new UrlEncodedFormEntity(pairs));

                            HttpResponse response = client.execute(put);
                            Log.v("--", response.getStatusLine()
                                    .getStatusCode() + " ST");
                        } catch (URISyntaxException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ClientProtocolException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

Im getting this issue on this line: HttpPut put = new HttpPut(url); Should I use some library to do so? Now im importing import org.apache.http.client.methods.HttpPut;


Solution

  • You have two options: create a URI instead a URL object, or just use the String:

    HttpPut put = new HttpPut(Constants.API_URL + "api/documents/"
                                    + mustRead.getContract_id()
                                    + "/mark_as_read");