Java 如何使用;上下文“;将一个类转换为另一个类-Android

Java 如何使用;上下文“;将一个类转换为另一个类-Android,java,android,class,android-context,Java,Android,Class,Android Context,我在一个类中使用上下文。在另一个类中启动时,如何引用相同的上下文 MyHttpClient.java public class MyHttpClient extends DefaultHttpClient { final Context contextkey; public MyHttpClient(Context contextkeystore) { this.contextkey = contextkeystore; } @Overri

我在一个类中使用上下文。在另一个类中启动时,如何引用相同的上下文

MyHttpClient.java

public class MyHttpClient extends DefaultHttpClient {


    final Context contextkey;

    public MyHttpClient(Context contextkeystore) {
        this.contextkey = contextkeystore;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        // Register for port 443 our SSLSocketFactory with our keystore
        // to the ConnectionManager
        registry.register(new Scheme("https", newSslSocketFactory(), 443));
        return new SingleClientConnManager(getParams(), registry);
    }

    private SSLSocketFactory newSslSocketFactory() {
        try {
            // Get an instance of the Bouncy Castle KeyStore format
            KeyStore trusted = KeyStore.getInstance("BKS");
            // Get the raw resource, which contains the keystore with
            // your trusted certificates (root and any intermediate certs)
            InputStream in = contextkey.getResources().openRawResource(R.raw.mykeystore);
            try {
                // Initialize the keystore with the provided trusted certificates
                // Also provide the password of the keystore
                trusted.load(in, "mysecret".toCharArray());
            } finally {
                in.close();
            }
            // Pass the keystore to the SSLSocketFactory. The factory is responsible
            // for the verification of the server certificate.
            SSLSocketFactory sf = new SSLSocketFactory(trusted);
            // Hostname verification from certificate
            // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
            sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
            return sf;
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
}
要在其他类中使用:

DefaultHttpClient client = new MyHttpClient(getApplicationContext()); ?? 

此处“getApplicationContext”不起作用,显示了一个错误

您需要一个有效的上下文来使用SSL扩展DefaultHttpClient

调用MyHttpClient构造函数的类似乎没有要传递给HttpClient的有效上下文

您应该通过该类将上下文传递到客户端:

  • 从应用程序启动的位置开始(启动程序活动、服务等)
  • 创建类(您还没有给我们一个名称),将getApplicationContext或ActivityCast(this)作为参数传递
  • 让您的类创建MyHttpClient,将所述上下文传递给构造函数
它说:

public class Main extends Activity
{
      private Foo myfoo;

      @Override
      protected void onCreate(Bundle icicle)
      {
             myfoo = new Foo(this); //Or myFoo = new Foo(getApplicationContext());   
      }
}

public class Foo
{
    private Context mycontext;
    private MyHttpClient myclient;

    public Foo(Context ctx)
    {
        mycontext = ctx;//No need to save the context if you aren't reusing it after this.
        myclient = new MyHttpClient(mycontext); 
    }
}

获取应用程序上下文的简单方法如下

package com.example.testactivity;

        public class App extends Application {
        public static Context context;

    public void onCreate() {
                    super.onCreate();
                    context=getApplicationContext();
                           }
    public static Context getcontext(){
     return context;
     }

   }
和在清单文件中 在应用程序标记中添加行

<application
        android:name="com.example.testactivity.App" 
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

Njoy:)

您从哪里调用getApplicationContext()?您可以发布在getApplicationContext()上引发的错误吗?我正在“DefaultHttpClient=new MyHttpClient(getApplicationContext())”中的另一个类中调用它;该类是否扩展了活动?你能在getApplicationContext()上发布错误日志吗?不,这个类不是活动类。没有错误,但它显示红色下划线,并要求我创建方法“getApplicationContext()
App.getcontext();