我正在开发 Android 应用程序,而一位朋友正在为一项服务开发更复杂的 .NET 服务器代码。我被告知现在,我通过 POST 发送给他的 JSON 数据以及我从他那里收到的更大的 JSON 数据必须通过 HTTPS。我发现的所有教程要么只是针对标准 HTTP,要么需要编写几个类来处理安全连接。我是 Android 开发的新手,所以这并不理想。我什至不确定我当前的代码是否会在没有安全连接的情况下从服务器中提取数据。
我从哪里开始?我需要做哪些改变?
我很乐意根据需要提供额外的信息,我已经沉迷于这段代码一个多星期了。索取信息时,请尽可能提供描述性信息。
HttpClient.java
public class HttpClient {
public static final String TAG = HttpClient.class.getSimpleName();
public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
// convert content stream to a String
String resultString= convertStreamToString(instream);
Log.v("After converting Stream to String", resultString);
instream.close();
// Transform the String into a JSONObject
JSONObject jsonObjRecv = new JSONObject(resultString);
// Raw DEBUG output of our received JSON object:
Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
return jsonObjRecv;
}
}
catch (Exception e)
{
// More about HTTP exception handling in another tutorial.
// For now we just print the stack trace.
e.printStackTrace();
}
return null;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*
* (c) public domain: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
*/
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();
}
}
LogCat 错误,从单击按钮开始,初始化一些 JSON 数据(已正确生成),然后使用 HttpClient.java:
08-29 12:47:16.697: W/ActivityThread(9547): Application com.barjinx.barjinx can be debugged on port 8100...
08-29 12:47:17.033: D/libEGL(9547): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-29 12:47:17.057: D/libEGL(9547): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-29 12:47:17.072: D/libEGL(9547): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-29 12:47:17.236: D/OpenGLRenderer(9547): Enabling debug mode 0
08-29 12:47:27.486: D/dalvikvm(9547): GC_CONCURRENT freed 92K, 2% free 9133K/9256K, paused 16ms+9ms, total 50ms
08-29 12:47:27.525: V/EventsActivity(9547): JSON Created: {"search":{"GeoLat":"29.7529","GeoZip":"78757","SearchTerm":"Rangers","GeoLong":"-97.7333","UserKey":"MattCoker"}}
08-29 12:47:36.361: I/HttpClient(9547): HTTPResponse received in [8826ms]
08-29 12:47:36.369: W/System.err(9547): org.json.JSONException: End of input at character 0 of
08-29 12:47:36.393: D/dalvikvm(9547): GC_CONCURRENT freed 230K, 3% free 9316K/9576K, paused 3ms+9ms, total 32ms
08-29 12:47:36.408: W/System.err(9547): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
08-29 12:47:36.408: W/System.err(9547): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
08-29 12:47:36.408: W/System.err(9547): at org.json.JSONObject.<init>(JSONObject.java:154)
08-29 12:47:36.408: W/System.err(9547): at org.json.JSONObject.<init>(JSONObject.java:171)
08-29 12:47:36.408: W/System.err(9547): at com.barjinx.barjinx.HttpClient.SendHttpPost(HttpClient.java:53)
08-29 12:47:36.408: W/System.err(9547): at com.barjinx.barjinx.EventsActivity$GetEventsDataTask.sendUserJSON(EventsActivity.java:172)
08-29 12:47:36.408: W/System.err(9547): at com.barjinx.barjinx.EventsActivity$GetEventsDataTask.doInBackground(EventsActivity.java:142)
08-29 12:47:36.408: W/System.err(9547): at com.barjinx.barjinx.EventsActivity$GetEventsDataTask.doInBackground(EventsActivity.java:1)
08-29 12:47:36.408: W/System.err(9547): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-29 12:47:36.408: W/System.err(9547): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-29 12:47:36.416: W/System.err(9547): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-29 12:47:36.416: W/System.err(9547): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-29 12:47:36.416: W/System.err(9547): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-29 12:47:36.416: W/System.err(9547): at java.lang.Thread.run(Thread.java:856)
08-29 12:47:36.416: W/dalvikvm(9547): threadid=11: thread exiting with uncaught exception (group=0x41e13930)
08-29 12:47:36.424: E/AndroidRuntime(9547): FATAL EXCEPTION: AsyncTask #1
08-29 12:47:36.424: E/AndroidRuntime(9547): java.lang.RuntimeException: An error occured while executing doInBackground()
08-29 12:47:36.424: E/AndroidRuntime(9547): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-29 12:47:36.424: E/AndroidRuntime(9547): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-29 12:47:36.424: E/AndroidRuntime(9547): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-29 12:47:36.424: E/AndroidRuntime(9547): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-29 12:47:36.424: E/AndroidRuntime(9547): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-29 12:47:36.424: E/AndroidRuntime(9547): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-29 12:47:36.424: E/AndroidRuntime(9547): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-29 12:47:36.424: E/AndroidRuntime(9547): at java.lang.Thread.run(Thread.java:856)
08-29 12:47:36.424: E/AndroidRuntime(9547): Caused by: java.lang.NullPointerException
08-29 12:47:36.424: E/AndroidRuntime(9547): at com.barjinx.barjinx.EventsActivity$GetEventsDataTask.sendUserJSON(EventsActivity.java:173)
08-29 12:47:36.424: E/AndroidRuntime(9547): at com.barjinx.barjinx.EventsActivity$GetEventsDataTask.doInBackground(EventsActivity.java:142)
08-29 12:47:36.424: E/AndroidRuntime(9547): at com.barjinx.barjinx.EventsActivity$GetEventsDataTask.doInBackground(EventsActivity.java:1)
08-29 12:47:36.424: E/AndroidRuntime(9547): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-29 12:47:36.424: E/AndroidRuntime(9547): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-29 12:47:36.424: E/AndroidRuntime(9547): ... 4 more
08-29 12:48:08.900: I/Process(9547): Sending signal. PID: 9547 SIG: 9
08-29 12:48:45.353: W/ActivityThread(9753): Application com.barjinx.barjinx is waiting for the debugger on port 8100...
08-29 12:48:45.361: I/System.out(9753): Sending WAIT chunk
08-29 12:48:45.486: I/dalvikvm(9753): Debugger is active
08-29 12:48:45.564: I/System.out(9753): Debugger has connected
08-29 12:48:45.564: I/System.out(9753): waiting for debugger to settle...
08-29 12:48:45.768: I/System.out(9753): waiting for debugger to settle...
08-29 12:48:45.971: I/System.out(9753): waiting for debugger to settle...
08-29 12:48:46.166: I/System.out(9753): waiting for debugger to settle...
08-29 12:48:46.369: I/System.out(9753): waiting for debugger to settle...
08-29 12:48:46.572: I/System.out(9753): waiting for debugger to settle...
08-29 12:48:46.768: I/System.out(9753): waiting for debugger to settle...
08-29 12:48:46.971: I/System.out(9753): debugger has settled (1326)
08-29 12:48:47.510: D/libEGL(9753): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-29 12:48:47.510: D/libEGL(9753): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-29 12:48:47.518: D/libEGL(9753): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-29 12:48:47.588: D/OpenGLRenderer(9753): Enabling debug mode 0
08-29 12:49:09.189: D/dalvikvm(9753): GC_CONCURRENT freed 80K, 2% free 9131K/9240K, paused 5ms+3ms, total 24ms
08-29 12:49:09.275: V/EventsActivity(9753): JSON Created: {"search":{"GeoLat":"29.7529","GeoZip":"78757","SearchTerm":"Rangers","GeoLong":"-97.7333","UserKey":"MattCoker"}}