gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:53:04 25 4
gpt4 key购买 nike

我的 Android java 代码有问题。我将发送一个 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);
}

}

这是我开始请求的主类:

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+"/");
}});
}

}

非常感谢您的帮助。

最佳答案

要查找的部分是堆栈跟踪的最后一个 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();的行。它正在创建一个 validator 并立即将其丢弃,因为它没有存储在变量中。

关于java - 在 android.os.AsyncTask$3.done(AsyncTask.java :300),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37478794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com