如何在android中使用HTTPPut

时间:2014-05-19 08:21:26

标签: java android apache androidhttpclient

我试图做一个HTTPPut,但似乎我无法实例化HTTPPut对象。我将此问题称为源代码,但似乎创建了一个HTTPPut对象,我需要一个URI而不是一个URL?有人能告诉我如何实例化HTTPPut对象吗?

来源:

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

我在这一行上遇到了这个问题:HttpPut put = new HttpPut(url);  我应该用一些图书馆这样做吗?现在我导入import org.apache.http.client.methods.HttpPut;

1 个答案:

答案 0 :(得分:0)

您有两种选择:创建URI而不是URL对象,或者只使用字符串:

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