1

私は Android アプリに取り組んでおり、友人はサービス用のはるかに複雑な .NET サーバー コードに取り組んでいます。現在、私が彼に POST で送信する JSON データと、私が彼から受け取るはるかに大きな JSON データは、HTTPS 経由である必要があると通知されました。私が見つけたすべてのチュートリアルは、標準の HTTP 用であるか、安全な接続を処理するためだけにいくつかのクラスを記述する必要があります。私はAndroid開発に慣れていないので、理想的ではありません。現在のコードが安全な接続なしでサーバーからデータを取得するかどうかさえわかりません。

どこから始めればよいですか?どのような変更を加える必要がありますか?

必要に応じて追加情報を提供させていただきます。私はこのコードに 1 週​​間以上夢中になっています。情報を要求するときは、できるだけ具体的に説明してください。

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"}}
4

1 に答える 1

0

SSL証明書がサーバーに正しくインストールされていることを確認してください。そのためにhttp://www.digicert.com/help/ツールを使用できます。

自己署名証明書を使用する場合は、次の手順に従う必要があります。

http://nelenkov.blogspot.com/2011/12/using-custom-certificate-trust-store-on.html

于 2013-08-29T18:56:41.027 に答える