在android.os.AsyncTask $ 3.done(AsyncTask.java:300)

时间:2016-05-27 08:40:02

标签: java android android-asynctask

我的Android代码存在问题。我将发送一个httpsRequest,然后我回来一个JSON文件。但我得到一个错误,我不知道为什么。我希望你能帮助我。

这是我得到的LogCat:

05-27 10:14:34.779: E/AndroidRuntime(24498): java.lang.RuntimeException: An error occured while executing doInBackground()
05-27 10:14:34.779: E/AndroidRuntime(24498):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at java.lang.Thread.run(Thread.java:818)
05-27 10:14:34.779: E/AndroidRuntime(24498): Caused by: java.lang.IllegalArgumentException: HostnameVerifier is null
05-27 10:14:34.779: E/AndroidRuntime(24498):    at javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(HttpsURLConnection.java:128)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at com.example.test.JSONTaskRegistrieren.doInBackground(JSONTaskRegistrieren.java:33)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at com.example.test.JSONTaskRegistrieren.doInBackground(JSONTaskRegistrieren.java:1)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-27 10:14:34.779: E/AndroidRuntime(24498):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-27 10:14:34.779: E/AndroidRuntime(24498):    ... 4 more

我的代码是

package com.example.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

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

    private static final HostnameVerifier HostnameVerifier = null;

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

        String Servicepass;
        String SecretToken;

        BufferedReader reader = null;
        URL url;

        try{
            url = new URL(params[0]);
            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);
        }

}

这是我的Mainclass,我开始请求:

package com.example.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


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://192.168.0.64";

                    // Hersteller Hash
                    String HHash = "fa9b0dec3776ba123eba3049ec9f398h754";

                    // AppHash
                    String HAHash = "d20439ab3eabf9ebc3fae2c89b4869ai967";

                    // AppID
                    int AppID = 1;


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

}

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

要查找的部分是堆栈跟踪的最后一个Caused by:部分。在您的情况下IllegalArgumentException: HostnameVerifier is null由您的代码at com.example.test.JSONTaskRegistrieren.doInBackground(JSONTaskRegistrieren.java:33)引起(这是第一行提及您的代码)

从这里你会发现有问题的代码行是

HttpsURLConnection.setDefaultHostnameVerifier(HostnameVerifier); 

并且该时间点HostnameVerifier变量的状态为null。当您检查该变量的分配位置时,您会看到您在初始化时分配内容的唯一时间。

private static final HostnameVerifier HostnameVerifier = null;

将其更改为

private static final HostnameVerifier HostnameVerifier = new NullHostnameVerifier();

并删除执行new NullHostnameVerifier();的行。它正在创建一个验证程序并立即将其抛出,因为它没有存储在变量中。

答案 1 :(得分:0)

我认为这个变量是原因

private static final HostnameVerifier HostnameVerifier = null;

你应该使用以下

url = new URL(params[0]);
HttpsURLConnection.setDefaultHostnameVerifier(url); 

答案 2 :(得分:0)

HttpsURLConnection.setDefaultHostnameVerifier(HostnameVerifier); 

此处发送的HostnameVerifier参数为null,如全局声明的那样!

这句话是什么?

new NullHostnameVerifier();

您想要HostnameVerfier = new NullHostnameVerifier();吗?