6
public class FetchImageAppsActivity extends Activity {
/** Called when the activity is first created. */
 public static String urlPath = "http://farm1.static.flickr.com/150/399390737_7a3d508730_b.jpg";
 public ImageView imageView;
 boolean usingProxy=true;
 public  String proxyIP="A.B.C.D";
 public  int proxyPort=80;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    imageView =(ImageView) findViewById(R.id.imageViewFetch);

    if(usingProxy==true){
     setProxy();
    }
    try {
        fetch(urlPath);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Drawable image = ImageOperations(this,urlPath,"overflow.png");
    imageView.setImageDrawable(image);


   /* imageView.setMinimumWidth();
    imageView.setMinimumHeight(height);

    imageView.setMaxWidth(width);
    imageView.setMaxHeight(height);
  */
}

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) ((FetchImageAppsActivity) ctx).fetch(url); //error line
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
try{
    URL url = new URL(address);
    URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
    url = uri.toURL();
    Log.i("Url:", url+"");
    Object content = url.getContent();
    return content;
    }catch(Exception e){
        e.printStackTrace();
    }
   return null;
   }

public void setProxy()
{ Uri uri = Uri.parse("http://farm1.static.flickr.com/150/399390737_7a3d508730_b.jpg");
  Properties systemProperties = System.getProperties();

  systemProperties.put( "proxySet", "true" );
  systemProperties.setProperty("http.proxyHost",proxyIP);
  systemProperties.setProperty("http.proxyPort","80");
  systemProperties.setProperty("http.nonProxyHosts",uri.getHost()); //uri.getHost()

HttpHost PROXY_HOST=null;
if( usingProxy == true)
     PROXY_HOST = new HttpHost(proxyIP, proxyPort); 
HttpClient client = null;
File f = null;
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(2);
HttpParams params = new BasicHttpParams();

params.setParameter(ExecutionContext.HTTP_TARGET_HOST, PROXY_HOST);

params.setBooleanParameter("http.protocol.expect-continue", false);
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(1));
ConnManagerParams.setMaxTotalConnections(params, 1);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUseExpectContinue(params, false);
HttpProtocolParams.setContentCharset(params,HTTP.DEFAULT_CONTENT_CHARSET);

HttpConnectionParams.setConnectionTimeout(params,50000);
HttpConnectionParams.setSoTimeout(params, 10000);
SchemeRegistry registry = new SchemeRegistry();

registry.register(new Scheme("https", PlainSocketFactory.getSocketFactory(),443));
//if (data != null && data.equals("data")) {
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(),proxyPort));    //uri.getPort()
   //   } else {
    registry.register(new Scheme("https", PlainSocketFactory.getSocketFactory(), proxyPort));
     // }
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry);
client = new DefaultHttpClient(cm, params);
//Proxy.NO_PROXY
if(usingProxy == true)
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, PROXY_HOST);
HttpContext ctx = new BasicHttpContext();
ctx.setAttribute(ExecutionContext.HTTP_TARGET_HOST, PROXY_HOST);
try {
    HttpPost request = new HttpPost(); 
    request.setParams(params);


    request.setURI(new URI(uri.toString()));
    request.setHeader("Content-Type","application/x-www-form-urlencoded");
}catch(Exception e){
    System.out.println(e.getMessage());
}
    }
    }

我在 android 模拟器中运行时出现异常,我的机器连接到启用代理的 WiFi 路由器,如下所示

java.net.UnknownHostException: Host is unresolved: farm1.static.flickr.com:80

错误日志:

09-05 13:44:48.817: WARN/System.err(489): java.net.UnknownHostException: Host is unresolved: farm1.static.flickr.com:80
09-05 13:44:48.839: WARN/System.err(489):     at java.net.Socket.connect(Socket.java:1038)
09-05 13:44:48.847: WARN/System.err(489):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
09-05 13:44:48.884: WARN/System.err(489):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
09-05 13:44:48.886: WARN/System.err(489):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
09-05 13:44:48.897: WARN/System.err(489):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
09-05 13:44:48.937: WARN/System.err(489):     at java.net.URLConnection.getContent(URLConnection.java:157)
09-05 13:44:48.937: WARN/System.err(489):     at java.net.URL.getContent(URL.java:621)
09-05 13:44:48.967: WARN/System.err(489):     at com.android.fetchimage.FetchImageAppsActivity.fetch(FetchImageAppsActivity.java:96)
09-05 13:44:48.967: WARN/System.err(489):     at com.android.fetchimage.FetchImageAppsActivity.onCreate(FetchImageAppsActivity.java:62)
09-05 13:44:48.987: WARN/System.err(489):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-05 13:44:48.987: WARN/System.err(489):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-05 13:44:48.987: WARN/System.err(489):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-05 13:44:48.987: WARN/System.err(489):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-05 13:44:48.987: WARN/System.err(489):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-05 13:44:48.987: WARN/System.err(489):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 13:44:48.987: WARN/System.err(489):     at android.os.Looper.loop(Looper.java:123)
09-05 13:44:48.987: WARN/System.err(489):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 13:44:49.028: WARN/System.err(489):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 13:44:49.028: WARN/System.err(489):     at java.lang.reflect.Method.invoke(Method.java:521)
09-05 13:44:49.028: WARN/System.err(489):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 13:44:49.047: WARN/System.err(489):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 13:44:49.067: WARN/System.err(489):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

3

设置文件Internet中的android权限Manifest.xml

android.permission.INTERNET

于 2012-09-05T10:09:55.133 回答
1

在不设置属性的情况下尝试相同的代码systemProperties.setProperty("http.proxyPort","80");

于 2012-09-05T09:48:30.283 回答
0

得到了解决方案:

String url = "farm1.static.flickr.com/150/399390737_7a3d508730_b.jpg"; proxy = "yourProxyIP", port = "yourProxyPort"; URL server = new URL(url); Properties systemProperties = System.getProperties(); systemProperties.setProperty("http.proxyHost",proxy); systemProperties.setProperty("http.proxyPort",port); HttpURLConnection connection = (HttpURLConnection)server.openConnection(); connection.connect(); InputStream in = connection.getInputStream(); image = Drawable.createFromStream(in, "src"); imageView.setImageDrawable(image);

对于使用“https”,请使用 SSL 身份验证

于 2013-10-15T10:51:00.003 回答