Android上没有与HTTPS站点的连接

时间:2016-06-09 09:06:44

标签: java android

我已经尝试在Java for Android中打开我的https链接,但它没有连接。在此之后,我尝试在新的Javaproject中打开我的链接,它正在连接。但我不明白为什么不在Android中。我希望有人可以帮助我。

我打开和连接的代码是:

public class JSONTaskRegistrieren extends AsyncTask<String, String, String>{

private static final HostnameVerifier HostnameVerifier = new NullHostnameVerifier();
private SSLSocketFactory socketfactory;

@Override
protected String doInBackground(String...params){

    String Servicepass;
    String SecretToken = null;


    BufferedReader reader = null;
    URL url;

    try{
        url = new URL(params[0]);
        this.socketfactory = HttpsURLConnection.getDefaultSSLSocketFactory();
        HttpsURLConnection.setDefaultHostnameVerifier((HostnameVerifier)  new NullHostnameVerifier());
        HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

        con.setRequestMethod("GET");
        con.setRequestProperty("Accept",  "application/json");

        con.setRequestProperty("WWSVC-REQID",  "1");

        InputStream stream = con.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        StringBuffer buffer = new StringBuffer();
        String output;

        while ((output = reader.readLine()) != null){
            buffer.append(output);
        }

        String finalJson = buffer.toString();
        //System.out.println(buffer.toString());

        JSONObject parentObject = new JSONObject(finalJson);
        JSONObject servicepassobjekt = parentObject.getJSONObject("SERVICEPASS");

        //System.out.println(servicepassobjekt.toString());
        StringBuffer finalBufferedData = new StringBuffer();

            JSONObject finalObject = servicepassobjekt;

            Servicepass = finalObject.getString("PASSID");
            SecretToken = finalObject.getString("APPID");

            return finalBufferedData.toString();

    } catch (MalformedURLException e) {
        e.printStackTrace();
     } catch (IOException e) {
        e.printStackTrace();
     } catch (JSONException e) {
       e.printStackTrace();
   }
    return null;
}

    @Override
    protected void onPostExecute(String result){
        super.onPostExecute(result);
        System.out.println(result);
    }

}   

在这里我构建了我的网址:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

    Button button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("unused")
            public void onClick(View v) {
                // ServiceURL
                String ServiceURL = "https://meine-webware.de";

                // Hersteller Hash
                String HHash = "fa9b0dec3776ba123eba3049ec9f398h";

                // AppHash
                String HAHash = "d20439ab3eabf9ebc3fae2c89b4869ai";

                // AppID
                int AppID = 1;


                new JSONTaskRegistrieren().execute(ServiceURL + "/WWSVC/WWSERVICE/REGISTER/"+HHash+"/"+HAHash+"/"+AppID);
            }});    
}

}

堆栈跟踪:

06-09 05:36:52.470: E/AndroidRuntime(1864): FATAL EXCEPTION: main
06-09 05:36:52.470: E/AndroidRuntime(1864): Process: com.example.test, PID: 1864
06-09 05:36:52.470: E/AndroidRuntime(1864): java.lang.NullPointerException
06-09 05:36:52.470: E/AndroidRuntime(1864):     at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at com.example.test.JSONTaskRegistrieren.onPostExecute(JSONTaskRegistrieren.java:86)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at com.example.test.JSONTaskRegistrieren.onPostExecute(JSONTaskRegistrieren.java:1)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at android.os.AsyncTask.finish(AsyncTask.java:632)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at android.os.Looper.loop(Looper.java:136)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at java.lang.reflect.Method.invoke(Method.java:515)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-09 05:36:52.470: E/AndroidRuntime(1864):     at dalvik.system.NativeStart.main(Native Method)

我知道这是NullPointerException,原因是,他无法连接此URL,但我不知道原因。

1 个答案:

答案 0 :(得分:0)

请勿使用System.out.printlnLog.v("JSONTaskRegistrieren", result);

并更改此部分,然后发布例外

} catch (MalformedURLException e) {
    Log.e("JSONTaskRegistrieren", "Error", e);
    e.printStackTrace();
 } catch (IOException e) {
    Log.e("JSONTaskRegistrieren", "Error", e);
    e.printStackTrace();
 } catch (JSONException e) {
    Log.e("JSONTaskRegistrieren", "Error", e);
   e.printStackTrace();