适用于Android httpPost的库

时间:2015-08-28 11:11:03

标签: android-studio

有点令我沮丧......我试图创建httpPost 代码:



private void makePostRequest() {


        HttpClient httpClient = new DefaultHttpClient();
        // replace with your url
        HttpPost httpPost = new HttpPost("www.example.com");


        //Post Data
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair("username", "test_user"));
        nameValuePair.add(new BasicNameValuePair("password", "123456789"));


        //Encoding POST data
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
            // log exception
            e.printStackTrace();
        }

        //making POST request.
        try {
            HttpResponse response = httpClient.execute(httpPost);
            // write response to log
            Log.d("Http Post Response:", response.toString());
        } catch (ClientProtocolException e) {
            // Log exception
            e.printStackTrace();
        } catch (IOException e) {
            // Log exception
            e.printStackTrace();
        }

    }
&#13;
&#13;
&#13;

但总是结束无法解决符号。我使用了Android Studio最新更新,我已经从hc.apache.org下载了http://mirror.metrocast.net/apache//httpcomponents/httpclient/binary/httpcomponents-client-4.5-bin.zip并将jar文件(httpclient-4.5.jar和httpcore-4.4.1.jar)复制到库并将其添加为库但不知何故它没有用。

它给出错误:

  
    

错误:任务&#39;:app:packageDebug&#39;执行失败。     在APK META-INF / DEPENDENCIES文件1中复制的重复文件:Z:\ Learning \ Android-2 \ Live-code-AS \ SendLocation \ app \ libs \ _ httpcore-4.4.1.jar       文件2:     Z:\学习\ Android的2 \活代码-AS \ SendLocation \应用\库\ HttpClient的-4.5.jar

  

如果我删除了一个jar文件,那么我将无法解决HttpClient或NameValuePair&#34;

请帮助......先谢谢。

1 个答案:

答案 0 :(得分:1)

您的依赖项中有一些重复文件。要解决此问题,请打开build.gradle,在其中定义依赖项并在android-section中添加packagingOptions-section:

android {
    packagingOptions {
       exclude 'META-INF/DEPENDENCIES'
    }
}