Cannot Resolve Symbol HttpEntity,HttpResponse
Asked Answered
E

3

5

I am trying to learn XML parsing using this tutorial but some of the classes are not getting imported. Here is the code:

public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse resp = client.execute(uri);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

These classes are not getting imported:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.DefaultHttpClient;

My Gradle properties:

android {
    compileSdkVersion 23
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.rr.rio"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
}
Eyrie answered 13/11, 2015 at 11:38 Comment(1)
Bad, bad... please stop using sockpupet accounts for serial upvoting.Vitiate
B
12

HTTP client is deprecated on sdk 23, use HttpURLConnection instead

or add this to your gradle (not recommended)

android {
    useLibrary 'org.apache.http.legacy'
}

EDIT

public String getXmlFromUrl(String urlString) {
    String xml = null;

    URL url;
    HttpURLConnection urlConnection = null;

    try {
        url = new URL(urlString);

        urlConnection = (HttpURLConnection) url.openConnection();

        InputStream in = urlConnection.getInputStream();

        InputStreamReader isw = new InputStreamReader(in);

        BufferedReader br = new BufferedReader(isw);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }
        br.close();

        xml = sb.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    // return XML
    return xml;
}

http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

Bobker answered 13/11, 2015 at 11:48 Comment(3)
Can we use HttpURLConnection for Xml Parsing? @BobkerEyrie
can u provide Link or Ideas plz! @BobkerEyrie
I'll make an example same as Android Hive but using HttpURLConnection, please wait.Bobker
E
3

You can add this in build.gradle(Module: app): HttpClient is not supported in sdk 23. Either you have to use URLConnection or downgrade to sdk 22.

OR

If you need sdk 23, add this to your gradle:

android {
    useLibrary 'org.apache.http.legacy'
}
Evie answered 12/7, 2016 at 6:53 Comment(0)
M
0

The package org.apache.http got removed in API 23. If you want to use it for learning, use a lower API, or find alternatives. See this link: HttpEntity is deprecated on Android now, what's the alternative?

Maidenhood answered 13/11, 2015 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.