Android:无法解析符号HttpPost,DefaultHttpClient,BasicNameValuePair,NameValuePair

时间:2016-07-04 12:10:01

标签: android build.gradle apache-httpclient-4.x

我有一个Android应用程序,我需要使用HTTP请求访问oauth服务.Below是我的gradle配置文件。

    apply plugin: 'com.android.application'

android {
  compileSdkVersion 23
  buildToolsVersion '23.0.2'
  useLibrary 'org.apache.http.legacy'

  defaultConfig {
    applicationId "com.example.MyProject"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    vectorDrawables.useSupportLibrary = true
  }
  buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
  packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
  }
}

  dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:cardview-v7:23.2.0'
compile 'com.android.support:recyclerview-v7:23.2.0'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile('org.springframework.android:spring-android-auth:1.0.1.RELEASE') {
  exclude module: 'spring-core'
}
compile 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
compile 'org.apache.commons:commons-io:1.3.2'
compile 'joda-time:joda-time:2.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.7.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.2'
compile 'com.google.code.gson:gson:2.6.2'
compile ('com.github.ganfra:material-spinner:1.1.1'){
  exclude group: 'com.android.support', module: 'appcompat-v7'
}

compile 'com.getbase:floatingactionbutton:1.10.1'
compile 'com.github.clans:fab:1.6.2'
compile files('libs/-2-2-mpandroidchartlibrary4.jar')
compile files('libs/achartengine-1.2.0.jar')

  }

下面是我正在发出HTTP请求的GetAccessToken类。

GetAccessToken.java:

      import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;

  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONException;
  import org.json.JSONObject;
  import android.util.Log;
  public class GetAccessToken {
  static InputStream is = null;
  static JSONObject jObj = null;
  static String json = "";
  public GetAccessToken() {
  }
  List<NameValuePair> params = new ArrayList<NameValuePair>();
  Map<String, String> mapn;
  DefaultHttpClient httpClient;
  HttpPost httpPost;

  public JSONObject gettoken(String address,String token,String client_id,String client_secret,String redirect_uri,String grant_type) {
      // Making HTTP request
      try {
      // DefaultHttpClient
      httpClient = new DefaultHttpClient();
      httpPost = new HttpPost(address);

      params.add(new BasicNameValuePair("code", token));
      params.add(new BasicNameValuePair("client_id", client_id));
      params.add(new BasicNameValuePair("client_secret", client_secret));
      params.add(new BasicNameValuePair("redirect_uri", redirect_uri));
      params.add(new BasicNameValuePair("grant_type", grant_type));

      httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
      httpPost.setEntity(new UrlEncodedFormEntity(params));
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();

      } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      } catch (ClientProtocolException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }
      try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          is, "iso-8859-1"), 8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
          sb.append(line + "\n");
      }
      is.close();

      json = sb.toString();
      Log.e("JSONStr", json);
      } catch (Exception e) {
          e.getMessage();
      Log.e("Buffer Error", "Error converting result " + e.toString());
      }
      // Parse the String to a JSON Object
      try {
      jObj = new JSONObject(json);
      } catch (JSONException e) {
      Log.e("JSON Parser", "Error parsing data " + e.toString());
      }
      // Return JSON String
      return jObj;
  }

  }

但是我的进口量低于正确率。

      import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;

我应该在我的graddle配置中添加什么来解决这些错误。

0 个答案:

没有答案