1

enter image description hereI'm new android development and I have been researching this for some time however I can't seem to correct the error. I have a "The Method is undefined for the type object" error on both getStatusCode() and getReasonPhrase(). Any help would be appreciated.

My code as fallows

package com.javapapers.java.io;  
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class HttpUtil {

    public String getHttpResponse(HttpRequestBase request) {
        String result = null;
        try {

            DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
            HttpResponse httpResponse = httpClient.execute(request);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            String reason = httpResponse.getStatusLine().getReasonPhrase();
            StringBuilder sb = new StringBuilder();
            if (statusCode == 200) {
                HttpEntity entity = httpResponse.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader bReader = new BufferedReader(
                        new InputStreamReader(inputStream, "UTF-8"), 8);
                String line = null;
                while ((line = bReader.readLine()) != null) {
                    sb.append(line);
                }
            } else {
                sb.append(reason);
            }
            result = sb.toString();
        } catch (UnsupportedEncodingException ex) {
        } catch (ClientProtocolException ex1) {
        } catch (IOException ex2) {
        }
        return result;
    }
}
Craig Gallagher
  • 225
  • 1
  • 5
  • 18
  • Have you checked your API level for android? – Nik Jan 22 '16 at 14:32
  • 1
    Are you aware of the fact that you [shouldn't use the Apache HTTP client](https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client) in an Android project anymore? – toKrause Jan 22 '16 at 14:33
  • @Nik I have added a photo of the API levels I'm using – Craig Gallagher Jan 22 '16 at 15:03
  • @toKrause what would my alternative be. I'm not completely sure how this code works as I'm fallowing a tutorial. Thank – Craig Gallagher Jan 22 '16 at 15:05
  • 1
    As mentioned in the linked document, you should resort to [HttpURLConnection](https://developer.android.com/reference/java/net/HttpURLConnection.html). [Here](https://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) is all you need to know. – toKrause Jan 22 '16 at 15:08
  • One more thing about the code you've provided: Always check caught exceptions. At least log the error message. It may reveal that `httpClient.execute(request)` has failed for some reason. – toKrause Jan 22 '16 at 15:12

0 Answers0