2016-05-02 62 views
0

在Android Studio中,我試圖導入的HttpResponseStringEntity,標識此我分別使用import org.apache.http.HttpResponseimport org.apache.http.entity.StringEntity。但android工作室並不iddentifies這些進口,並顯示cannot resolve symbol。那麼我怎樣才能在我的項目中使用這些庫?的Android Studio不HttpResponse對象及StringEntity

這是我的代碼(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; 
     } 
    } 

} 
+0

in api 23 google不支持org.apache.http你必須在你的項目中手動添加 –

+0

在這裏發佈你的'Gradle'代碼。看起來像'Gradle'中缺少的東西。 –

回答

-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

+1

這似乎是你[複製此答案](http://stackoverflow.com/a/32066606/2308683) –

+0

是的,因爲它適用於我 –

+0

然後,請將問題標記爲重複,而不是爲他人的答案 –

0

下載這些庫:enter image description here

然後將其粘貼在你的項目文件夾中>>應用> LIB >>這裏

然後去AS中,右鍵單擊項目>>開放模塊設置>>依賴項TAB >>點擊+ >>添加文件依賴項>>找到lib文件夾並導入其中的所有三個。並且你完成了!