对于类型对象错误,未定义方法

时间:2016-01-22 19:39:15

标签: java android eclipse

我一直在研究这个但是我似乎无法纠正错误。我只是在Android开发中的新手。我在product1 { float: left; width: 100%; height: 100%; margin-bottom: 8px; border: 1px solid #EEE; min-height: 300px; background-color: #FFF; box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.05) inset; position: relative; } .product1 .image a img { max-width: 100%; max-height: 178px; } getStatusCode()上都有“未定义类型对象的方法”错误。任何帮助将不胜感激。

getReasonPhrase()

然后告诉我,我不应该使用Apache HTTP客户端,而是使用HttpURLConnection,所以我改变了我的代码,我想知道这是否有效,结果是否相同

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

甚至是这样的

public class HttpUtil {
    public static void main(String[] args) throws Exception {

        if (args.length != 2) {
            System.err.println("Usage:  java Reverse "
                + "https://twitter.com/aaroadwatch"
                + " string_to_reverse");
            System.exit(1);
        }

        String stringToReverse = URLEncoder.encode(args[1], "UTF-8");

        URL url = new URL(args[0]);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(
                                         connection.getOutputStream());
        out.write("string=" + stringToReverse);
        out.close();

        BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    connection.getInputStream()));
        String decodedString;
        while ((decodedString = in.readLine()) != null) {
            System.out.println(decodedString);
        }
        in.close();
    }
}

我的Twitter API类 我在 URL url = new URL("https://twitter.com/aaroadwatch"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); finally { urlConnection.disconnect(); } } getHttpResponse上出错     包com.javapapers.social.twitter;

getHttpResponse

1 个答案:

答案 0 :(得分:2)

这是一种利用HttpUrlConnection的可能实现:

private static final int CONNECTION_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(15);
private static final int READ_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(15);

public static void main(String[] args) {
    String address = "https://twitter.com/aaroadwatch";
    System.out.println(get(address));
}

public static String get(String address) {
    String result = null;
    HttpURLConnection conn = null;
    InputStream in = null;
    try {
        // building api url
        URL url = new URL(address);
        System.out.println("GET URL " + url.toString());
        // establishing connection with server
        conn = (HttpURLConnection) url.openConnection();
        // building headers
        conn.setReadTimeout(READ_TIMEOUT);
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            in = new BufferedInputStream(conn.getInputStream());
            // building output string from stream
            StringBuilder sb = new StringBuilder();
            int b;
            while ((b = in.read()) != -1) {
                sb.append((char) b);
            }
            String output = sb.toString().replace("\n", "");
            System.out.println("GET RES " + output);
            result = output;
        }
    } catch (MalformedURLException ex) {
        System.err.println("malformed url");
        ex.printStackTrace();
    } catch (IOException ex) {
        System.err.println("I/O exception");
        ex.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
            }
        }
        if (conn != null) {
            conn.disconnect();
        }
    }
    return result;
}

希望这可以提供帮助。

相关问题