我遵循本教程:
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
未定义
答案 0 :(得分:2)
您应该在实例化类时发送活动的上下文:
private Context mContext;
public JsonReaderPost(Context mContext) {
this.mContext = mContext;
}
然后,你应该使用&#34; mContext&#34;而不是getApplicationContext();
答案 1 :(得分:1)
这是未知的,因为您的班级没有扩展任何其他Class
Context
,因此它不知道该方法是什么。例如,这是Activity
。
但是,使用getApplicationContext()
,除非您真的知道您正在做什么,否则几乎总是错误的。如果处理不当,这会带来Exception
之类的不良行为。您应该始终使用您正在处理的课程的Context
。
您可以了解哪些类实现Context
并获取有关上下文here的更多信息。