我试图从https客户端获取Json数据。我知道如何从http客户端获取Json数据。我想知道有什么办法吗?
当我使用api.androidhive.info/contacts作为json url时,它正在运行。但是,当我使用jsonblob.com/api/jsonBlob/56aa6129e4b01190df4c0b87作为json url时,它不是。因为前者是http,后者是https。
我使用了okHttp客户端,但它没有用。
这是我的MainActivity
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
final int CONN_TIME = 1000 * 15;
final String SERVER_ADDRESS = "http://api.androidhive.info/contacts/";
TextView txtView;
String ans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView)findViewById(R.id.screen);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait...");
progressDialog.show();
SyncUserDataAsyncTask s = new SyncUserDataAsyncTask();
s.execute();
}
public class SyncUserDataAsyncTask extends AsyncTask<Void, Void, JSONArray> {
// User user;
SyncUserDataAsyncTask() {
}
@Override
protected JSONArray doInBackground(Void... params) {
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONN_TIME);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONN_TIME);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS);
JSONArray returnedArray = null;
try {
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
ans = result;
JSONArray jArray = new JSONArray(result);
if (jArray.length() == 0) {
returnedArray = null;
} else {
returnedArray = jArray;
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedArray;
}
@Override
protected void onPostExecute(JSONArray returnedArray) {
progressDialog.dismiss();
// callBack.done(returnedArray);
//super.onPostExecute(returnedArray);
txtView.setText(ans);
}
}
}
更新了MainActivity
import java.io.IOException;
import java.security.cert.CertificateException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.conn.ssl.SSLSocketFactory;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
final int CONN_TIME = 1000 * 15;
final String SERVER_ADDRESS = "jsonblob.com/api/jsonBlob/56aa6129e4b01190df4c0b87";
TextView txtView;
String ans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView) findViewById(R.id.screen);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait...");
progressDialog.show();
SyncUserDataAsyncTask s = new SyncUserDataAsyncTask();
s.execute();
}
public class SyncUserDataAsyncTask extends AsyncTask<Void, Void, String> {
// User user;
SyncUserDataAsyncTask() {
}
@Override
protected String doInBackground(Void... params) {
try {
OkHttpClient client = getUnsafeOkHttpClient();
Request request = new Request.Builder().url(SERVER_ADDRESS)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
@Override
protected void onPostExecute(String returnedArray) {
progressDialog.dismiss();
// callBack.done(returnedArray);
// super.onPostExecute(returnedArray);
if (!returnedArray.matches(""))
txtView.setText(returnedArray);
}
}
private static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts,
new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext
.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
logcat的
02-10 15:57:40.855: I/ActivityManager(353): Displayed com.assignment.dipper/.MainActivity: +5s99ms
02-10 15:57:43.741: W/art(1459): Verification of boolean okhttp3.internal.Util.skipAll(okio.Source, int, java.util.concurrent.TimeUnit) took 150.654ms
02-10 15:57:43.801: I/Choreographer(1459): Skipped 69 frames! The application may be doing too much work on its main thread.
02-10 15:57:43.857: I/Choreographer(1459): Skipped 31 frames! The application may be doing too much work on its main thread.
02-10 15:57:43.986: W/art(1459): Verification of java.lang.String okhttp3.internal.Util.toHumanReadableAscii(java.lang.String) took 234.971ms
02-10 15:57:44.097: I/Choreographer(1459): Skipped 40 frames! The application may be doing too much work on its main thread.
02-10 15:57:44.207: I/Choreographer(1459): Skipped 52 frames! The application may be doing too much work on its main thread.
02-10 15:57:44.298: I/Choreographer(1459): Skipped 31 frames! The application may be doing too much work on its main thread.
02-10 15:57:44.407: I/Choreographer(1459): Skipped 36 frames! The application may be doing too much work on its main thread.
02-10 15:57:44.736: I/Choreographer(1459): Skipped 36 frames! The application may be doing too much work on its main thread.
02-10 15:57:44.987: E/AndroidRuntime(1459): FATAL EXCEPTION: AsyncTask #1
02-10 15:57:44.987: E/AndroidRuntime(1459): Process: com.assignment.dipper, PID: 1459
02-10 15:57:44.987: E/AndroidRuntime(1459): java.lang.RuntimeException: An error occured while executing doInBackground()
02-10 15:57:44.987: E/AndroidRuntime(1459): at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-10 15:57:44.987: E/AndroidRuntime(1459): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-10 15:57:44.987: E/AndroidRuntime(1459): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-10 15:57:44.987: E/AndroidRuntime(1459): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-10 15:57:44.987: E/AndroidRuntime(1459): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-10 15:57:44.987: E/AndroidRuntime(1459): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-10 15:57:44.987: E/AndroidRuntime(1459): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-10 15:57:44.987: E/AndroidRuntime(1459): at java.lang.Thread.run(Thread.java:818)
02-10 15:57:44.987: E/AndroidRuntime(1459): Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to get length of null array
02-10 15:57:44.987: E/AndroidRuntime(1459): at com.assignment.dipper.MainActivity.getUnsafeOkHttpClient(MainActivity.java:125)
02-10 15:57:44.987: E/AndroidRuntime(1459): at com.assignment.dipper.MainActivity.access$0(MainActivity.java:82)
02-10 15:57:44.987: E/AndroidRuntime(1459): at com.assignment.dipper.MainActivity$SyncUserDataAsyncTask.doInBackground(MainActivity.java:60)
02-10 15:57:44.987: E/AndroidRuntime(1459): at com.assignment.dipper.MainActivity$SyncUserDataAsyncTask.doInBackground(MainActivity.java:1)
02-10 15:57:44.987: E/AndroidRuntime(1459): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-10 15:57:44.987: E/AndroidRuntime(1459): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-10 15:57:44.987: E/AndroidRuntime(1459): ... 4 more
02-10 15:57:44.987: E/AndroidRuntime(1459): Caused by: java.lang.NullPointerException: Attempt to get length of null array
02-10 15:57:44.987: E/AndroidRuntime(1459): at okhttp3.internal.tls.RealTrustRootIndex.<init>(RealTrustRootIndex.java:31)
02-10 15:57:44.987: E/AndroidRuntime(1459): at okhttp3.internal.Platform.trustRootIndex(Platform.java:97)
02-10 15:57:44.987: E/AndroidRuntime(1459): at okhttp3.internal.Platform$Android.trustRootIndex(Platform.java:271)
02-10 15:57:44.987: E/AndroidRuntime(1459): at okhttp3.OkHttpClient.<init>(OkHttpClient.java:189)
02-10 15:57:44.987: E/AndroidRuntime(1459): at okhttp3.OkHttpClient.<init>(OkHttpClient.java:60)
02-10 15:57:44.987: E/AndroidRuntime(1459): at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:718)
02-10 15:57:44.987: E/AndroidRuntime(1459): at com.assignment.dipper.MainActivity.getUnsafeOkHttpClient(MainActivity.java:123)
02-10 15:57:44.987: E/AndroidRuntime(1459): ... 9 more
02-10 15:57:45.085: W/ActivityManager(353): Force finishing activity com.assignment.dipper/.MainActivity
02-10 15:57:45.146: I/Choreographer(1459): Skipped 67 frames! The application may be doing too much work on its main thread.
02-10 15:57:45.297: I/Choreographer(1459): Skipped 56 frames! The application may be doing too much work on its main thread.
02-10 15:57:45.407: I/Choreographer(1459): Skipped 65 frames! The application may be doing too much work on its main thread.
02-10 15:57:45.513: I/Choreographer(1459): Skipped 62 frames! The application may be doing too much work on its main thread.
02-10 15:57:45.584: I/Choreographer(1459): Skipped 41 frames! The application may be doing too much work on its main thread.
02-10 15:57:45.605: D/gralloc(52): Registering a buffer in the process that created it. This may cause memory ordering problems.
02-10 15:57:45.619: E/libEGL(52): called unimplemented OpenGL ES API
02-10 15:57:45.620: E/SurfaceFlinger(52): glCheckFramebufferStatusOES error 0
02-10 15:57:45.620: E/SurfaceFlinger(52): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
02-10 15:57:45.759: W/WindowManager(353): Screenshot failure taking screenshot for (320x480) to layer 21015
02-10 15:57:45.760: E/ActivityManager(353): Invalid thumbnail dimensions: 192x192
02-10 15:57:45.890: I/Choreographer(1459): Skipped 111 frames! The application may be doing too much work on its main thread.
02-10 15:57:46.714: I/Choreographer(1459): Skipped 530 frames! The application may be doing too much work on its main thread.
02-10 15:57:46.822: I/Choreographer(353): Skipped 388 frames! The application may be doing too much work on its main thread.
02-10 15:57:46.917: I/Choreographer(1459): Skipped 118 frames! The application may be doing too much work on its main thread.
02-10 15:57:47.287: I/Choreographer(1459): Skipped 231 frames! The application may be doing too much work on its main thread.
02-10 15:57:47.367: I/Choreographer(1459): Skipped 36 frames! The application may be doing too much work on its main thread.
02-10 15:57:47.381: I/Choreographer(353): Skipped 81 frames! The application may be doing too much work on its main thread.
02-10 15:57:47.564: I/Choreographer(1459): Skipped 43 frames! The application may be doing too much work on its main thread.
02-10 15:57:47.664: I/Choreographer(1459): Skipped 50 frames! The application may be doing too much work on its main thread.
02-10 15:57:47.845: I/Choreographer(1459): Skipped 89 frames! The application may be doing too much work on its main thread.
02-10 15:57:48.105: I/Choreographer(628): Skipped 1189 frames! The application may be doing too much work on its main thread.
02-10 15:57:48.181: I/Choreographer(1459): Skipped 144 frames! The application may be doing too much work on its main thread.
02-10 15:57:48.404: I/Choreographer(1459): Skipped 86 frames! The application may be doing too much work on its main thread.
02-10 15:57:48.591: I/Choreographer(353): Skipped 187 frames! The application may be doing too much work on its main thread.
02-10 15:57:48.648: I/Choreographer(1459): Skipped 152 frames! The application may be doing too much work on its main thread.
02-10 15:57:48.805: I/Choreographer(1459): Skipped 95 frames! The application may be doing too much work on its main thread.
02-10 15:57:48.895: I/Choreographer(353): Skipped 73 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.030: I/Choreographer(353): Skipped 61 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.020: I/Choreographer(1459): Skipped 74 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.171: I/Choreographer(353): Skipped 79 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.237: I/Choreographer(1459): Skipped 48 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.331: I/Choreographer(353): Skipped 1612 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.360: I/Choreographer(353): Skipped 107 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.429: I/Choreographer(1459): Skipped 103 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.521: I/Choreographer(353): Skipped 93 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.627: I/Choreographer(1459): Skipped 116 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.728: I/Choreographer(353): Skipped 122 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.803: I/Choreographer(353): Skipped 37 frames! The application may be doing too much work on its main thread.
02-10 15:57:49.929: I/Choreographer(353): Skipped 70 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.043: I/Choreographer(353): Skipped 62 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.207: I/Choreographer(353): Skipped 101 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.342: I/Choreographer(1459): Skipped 91 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.457: I/Choreographer(1459): Skipped 69 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.587: I/Choreographer(353): Skipped 543 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.655: I/Choreographer(1459): Skipped 118 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.818: I/Choreographer(353): Skipped 178 frames! The application may be doing too much work on its main thread.
02-10 15:57:50.947: I/Choreographer(1459): Skipped 123 frames! The application may be doing too much work on its main thread.
02-10 15:57:51.055: I/Choreographer(353): Skipped 296 frames! The application may be doing too much work on its main thread.
02-10 15:57:51.079: I/Choreographer(353): Skipped 139 frames! The application may be doing too much work on its main thread.
02-10 15:57:51.284: I/Choreographer(1459): Skipped 209 frames! The application may be doing too much work on its main thread.
02-10 15:57:51.460: I/Choreographer(1459): Skipped 110 frames! The application may be doing too much work on its main thread.
02-10 15:57:51.548: I/Choreographer(628): Skipped 633 frames! The application may be doing too much work on its main thread.
02-10 15:57:51.631: I/Choreographer(1459): Skipped 105 frames! The application may be doing too much work on its main thread.
02-10 15:57:51.890: I/Choreographer(353): Skipped 405 frames! The application may be doing too much work on its main thread.
02-10 15:57:52.029: I/Choreographer(353): Skipped 78 frames! The application may be doing too much work on its main thread.
02-10 15:57:52.216: I/Choreographer(353): Skipped 64 frames! The application may be doing too much work on its main thread.
02-10 15:57:52.738: I/Choreographer(353): Skipped 31 frames! The application may be doing too much work on its main thread.
02-10 15:57:52.922: I/Choreographer(1459): Skipped 269 frames! The application may be doing too much work on its main thread.
02-10 15:57:52.984: I/Choreographer(353): Skipped 72 frames! The application may be doing too much work on its main thread.
02-10 15:57:53.247: I/Choreographer(1459): Skipped 74 frames! The application may be doing too much work on its main thread.
02-10 15:57:53.341: I/Choreographer(353): Skipped 81 frames! The application may be doing too much work on its main thread.
02-10 15:57:53.437: I/Choreographer(1459): Skipped 52 frames! The application may be doing too much work on its main thread.
02-10 15:57:53.596: I/Choreographer(1459): Skipped 38 frames! The application may be doing too much work on its main thread.
02-10 15:57:53.681: I/Choreographer(353): Skipped 37 frames! The application may be doing too much work on its main thread.
02-10 15:57:53.759: I/Choreographer(353): Skipped 45 frames! The application may be doing too much work on its main thread.
02-10 15:57:54.064: I/Choreographer(1459): Skipped 35 frames! The application may be doing too much work on its main thread.
02-10 15:57:54.349: E/WindowManager(1459): android.view.WindowLeaked: Activity com.assignment.dipper.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{22cc757d V.E..... R.....ID 0,0-187,166} that was originally added here
02-10 15:57:54.349: E/WindowManager(1459): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
02-10 15:57:54.349: E/WindowManager(1459): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
02-10 15:57:54.349: E/WindowManager(1459): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.Dialog.show(Dialog.java:298)
02-10 15:57:54.349: E/WindowManager(1459): at com.assignment.dipper.MainActivity.onCreate(MainActivity.java:43)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.Activity.performCreate(Activity.java:5937)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-10 15:57:54.349: E/WindowManager(1459): at android.os.Handler.dispatchMessage(Handler.java:102)
02-10 15:57:54.349: E/WindowManager(1459): at android.os.Looper.loop(Looper.java:135)
02-10 15:57:54.349: E/WindowManager(1459): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-10 15:57:54.349: E/WindowManager(1459): at java.lang.reflect.Method.invoke(Native Method)
02-10 15:57:54.349: E/WindowManager(1459): at java.lang.reflect.Method.invoke(Method.java:372)
02-10 15:57:54.349: E/WindowManager(1459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-10 15:57:54.349: E/WindowManager(1459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-10 15:57:54.374: I/Choreographer(1459): Skipped 125 frames! The application may be doing too much work on its main thread.
02-10 15:57:54.557: W/WindowManager(353): Failed looking up window
02-10 15:57:54.557: W/WindowManager(353): java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@2aacfcff does not exist
02-10 15:57:54.557: W/WindowManager(353): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8412)
02-10 15:57:54.557: W/WindowManager(353): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:8403)
02-10 15:57:54.557: W/WindowManager(353): at com.android.server.wm.WindowManagerService.removeWindow(WindowManagerService.java:2558)
02-10 15:57:54.557: W/WindowManager(353): at com.android.server.wm.Session.remove(Session.java:186)
02-10 15:57:54.557: W/WindowManager(353): at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:197)
02-10 15:57:54.557: W/WindowManager(353): at com.android.server.wm.Session.onTransact(Session.java:130)
02-10 15:57:54.557: W/WindowManager(353): at android.os.Binder.execTransact(Binder.java:446)
答案 0 :(得分:5)
private static OkHttpClient getUnsafeOkHttpClient() {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
};
// Install the all-trusting trust manager
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
}
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = client.newBuilder();
builder.sslSocketFactory(sslSocketFactory);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return builder.build();
}