android studio不识别HttpResponse& StringEntity

时间:2016-05-02 17:19:39

标签: android android-studio

在android studio中我试图导入 HttpResponse StringEntity ,为此我分别使用import org.apache.http.HttpResponseimport org.apache.http.entity.StringEntity。但android studio不会识别这些导入并显示cannot resolve symbol。那么如何在我的项目中使用这些库?

这是我的代码( .java文件

package com.example.abc.project.MongoHQ;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;

import com.example.abc.project.Task;

public class SaveAsyncTask extends AsyncTask<Task, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Task... arg0) {
        try
        {
            Task task = arg0[0];

            QueryBuilder qb = new QueryBuilder();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost request = new HttpPost(qb.buildContactsSaveURL());

            StringEntity params =new StringEntity(qb.createContact(task));
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            if(response.getStatusLine().getStatusCode()<205)
            {
                return true;
            }
            else
            {
                return false;
            }
        } catch (Exception e) {
            //e.getCause();
            String val = e.getMessage();
            String val2 = val;
            return false;
        }
    }

}

2 个答案:

答案 0 :(得分:0)

下载这些库:enter image description here

然后将它们粘贴到您的项目文件夹&gt;&gt; app&gt; lib&gt;&gt; here

然后转到AS,右键单击项目&gt;&gt;打开模块设置&gt;&gt; dependecies TAB&gt;&gt;点击+&gt;&gt;添加文件依赖关系&gt;&gt;找到lib文件夹并导入所有这三个文件夹。你就完成了!

答案 1 :(得分:-1)

首先,您必须在libs文件夹中检查

Make sure in Libs Folder Check that apache library

Then add into your gradle file like this 

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.2'

        defaultConfig {
            applicationId "info.tranetech.laundry"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    android {
        useLibrary 'org.apache.http.legacy'
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1
        compile 'com.android.support:design:23.0.1
        testCompile 'junit:junit:4.12'
        compile files('libs/android-async-http-1.4.4.jar')
        compile 'com.google.android.gms:play-services:8.4.0'
    }

This Is my gradle file

  [1]: http://i.stack.imgur.com/V6B4y.png
  [2]: http://i.stack.imgur.com/6VxrE.png

Also check external library