0

I'm following this tutorial :

http://www.tutos-android.com/importer-ajouter-certificat-ssl-auto-signe-bouncy-castle-android/comment-page-2#comment-2159 (SSl auto signed certificate problem)

JsonParserFUnction code :

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

    }

I get in this line an error :

HttpClient client =new MyHttpClient(getApplicationContext());

The error is : The method getApplicationContext() is undefined for the type JsonReaderPost

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
Kin2Park
  • 23
  • 6

2 Answers2

2

You should send the context of your activity when you instantiate the class:

private Context mContext;

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

Then, you should use "mContext" instead of getApplicationContext();

SolArabehety
  • 8,467
  • 5
  • 38
  • 42
1

It is unknown because your class doesn't extend any other Class that has a Context, so it doesn't know what that method is. Such is, for example, an Activity.

However, using getApplicationContext(), unless you really know what you're doing, is almost always wrong. This will bring undesired behaviors like Exceptions when handled not properly. You should always use the Context of the class you're handling.

You can know which classes implement Context and get more info on contexts here.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • i'm only following what's written in the tutorial link – Kin2Park Apr 22 '14 at 18:53
  • Well, not all tutorials have good examples (the official ones always do), you can find many questions here on `SO` where you'll see the same about the `Context`, for example here: http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – nKn Apr 22 '14 at 18:57