0

I have an android application where i need to access an oauth sevice using HTTP request.Below is my gradle configuration file.

    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')

  }

Below is my GetAccessToken class where i am making HTTP request.

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

  }

But i am getting error for below imports.

      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;

What should i add in my graddle configuration to solve these errors.

KJEjava48
  • 1,967
  • 7
  • 40
  • 69
  • Did you check this follwoing question? It seems that you must edit top-level build.gradle as well (and not only app's module build.gradle): http://stackoverflow.com/questions/30856785/how-to-add-apache-http-api-legacy-as-compile-time-dependency-to-build-grade-fo – guipivoto Jul 04 '16 at 12:15
  • this is my gradle version classpath 'com.android.tools.build:gradle:2.0.+' – KJEjava48 Jul 04 '16 at 12:19
  • Are you able to do `sync`? `Tools > Android > "Sync Project with Gradle Files"` – Madhukar Hebbar Jul 04 '16 at 12:33
  • yes...these files are not importing after the sync – KJEjava48 Jul 04 '16 at 12:43
  • invoke this dependecy `compile org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2` – Fevly Pallar Jul 04 '16 at 13:33
  • http://stackoverflow.com/questions/31433687/android-gradle-apache-httpclient-does-not-exist review that one .. dependencys.compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5' android.useLibrary 'org.apache.http.legacy' those 2 in gradle.build – Robert Rowntree Jul 05 '16 at 22:40

0 Answers0