2014-04-22 42 views
0

我下面這個教程:getApplicationContext()問題

http://www.tutos-android.com/importer-ajouter-certificat-ssl-auto-signe-bouncy-castle-android/comment-page-2#comment-2159 (SSL自簽名證書問題)

JsonParserFUnction代碼:

package com.example.androidsupervision; 

    import java.io.BufferedReader; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.security.KeyManagementException; 
    import java.security.KeyStoreException; 
    import java.security.NoSuchAlgorithmException; 
    import java.security.UnrecoverableKeyException; 
    import java.security.cert.CertificateException; 
    import java.util.ArrayList; 
    import java.util.List; 

    import javax.net.ssl.HostnameVerifier; 
    import javax.net.ssl.HttpsURLConnection; 

    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.NameValuePair; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.entity.UrlEncodedFormEntity; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.conn.scheme.Scheme; 
    import org.apache.http.conn.scheme.SchemeRegistry; 
    import org.apache.http.conn.ssl.SSLSocketFactory; 
    import org.apache.http.conn.ssl.X509HostnameVerifier; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.apache.http.impl.conn.SingleClientConnManager; 
    import org.apache.http.message.BasicNameValuePair; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.content.Context; 
    import android.util.Log; 

    public class JsonReaderPost { 

     public JsonReaderPost() { 

     } 

     public void Reader() throws IOException, JSONException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException { 



      String ints = ""; 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("query","SELECT+AlertId+FROM+Orion.Alerts")); 
      //HttpClient client = new DefaultHttpClient(); 

    **//Here is the problem** 
      HttpClient client =new MyHttpClient(getApplicationContext()); 

      HttpPost httpPost = new 
HttpPost("https://192.168.56.101:17778/SolarWinds/InformationService/v3/Json/Query"); 
      httpPost.addHeader("content-type", "application/json"); 
      httpPost.addHeader("Authorization", "Basic YWRtaW46"); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 


      HttpResponse response; 
      String result = null; 

      response = client.execute(httpPost); 
      HttpEntity entity = response.getEntity(); 

      if (entity != null) { 

       // A Simple JSON Response Read 
       InputStream instream = entity.getContent(); 
       result = convertStreamToString(instream); 
       // now you have the string representation of the HTML request 
       // System.out.println("RESPONSE: " + result); 
       Log.e("Result", "RESPONSE: " + result); 
       instream.close(); 
      } 

      // Converting the String result into JSONObject jsonObj and then into 
      // JSONArray to get data 
      JSONObject jsonObj = new JSONObject(result); 
      JSONArray results = jsonObj.getJSONArray("results"); 
      for (int i = 0; i < results.length(); i++) { 
       JSONObject r = results.getJSONObject(i); 
       ints = r.getString("AlertId"); 
       Log.e("Final Result", "RESPONSE: " + ints); 
      } 

     } 


     public static String convertStreamToString(InputStream is) { 

      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      try { 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      return sb.toString(); 
     } 

    } 

我在這一行的錯誤得到:

HttpClient client =new MyHttpClient(getApplicationContext()); 

的錯誤是:方法getApplicationContext()是未定義類型JsonReaderPost

回答

2

當你實例化類你應該把你的活動範圍:

private Context mContext; 

public JsonReaderPost(Context mContext) { 
this.mContext = mContext; 
} 

然後,你應該使用「mContext」,而不是getApplicationContext();

+0

THX非常有幫助的,這就是它,現在仍然是證書問題 – Kin2Park

1

它是未知的,因爲你的類沒有延伸具有Context任何其他Class,所以它不知道的方法是什麼。例如,這是一個Activity

但是,使用getApplicationContext(),除非你知道你正在做什麼,幾乎總是錯的。當處理不正常這將帶來不期望的行爲,如Exception秒。您應該始終使用您正在處理的課程的Context

你可以知道哪些類實現Context,並獲得上下文here更多信息。

+0

我只是下面什麼寫的教程鏈接 – Kin2Park

+0

嗯,不是所有的教程都有很好的例子(官方的人總是這樣),你可以找到'SO'這裏的許多問題,在那裏你會看到有關相同'Context',例如這裏:http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – nKn

+0

謝謝,我會看到 – Kin2Park

相關問題