gpt4 book ai didi

java - 如何修复 android.os.NetworkOnMainThread

转载 作者:行者123 更新时间:2023-12-02 01:31:36 25 4
gpt4 key购买 nike

我收到"android.os.NetworkOnMainThreadException"即使我没有在主线程中运行任何与网络相关的内容。我该如何解决这个问题?

我实际上在 Eclipse 中尝试了代码,它运行得很好,但在我正在开发应用程序本身的 Android Studio 中却不行。

测试类.java:

package com.*****.*****;



import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.xpath.*;
import javax.xml.namespace.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

abstract class Testclass {


public static class NamespaceResolver implements NamespaceContext {
private Document document;

public NamespaceResolver(Document doc) {
document = doc;
}

public String getNamespaceURI(String prefix) {
if (prefix.equals("")) {
return document.lookupNamespaceURI(null);
} else {
return document.lookupNamespaceURI(prefix);
}
}

public String getPrefix(String namespaceURI) {
return document.lookupPrefix(namespaceURI);
}

public Iterator<String> getPrefixes(String namespaceURI) {
return null;
}
}

public static String downloadString(String url) throws Exception {
StringBuilder sb = new StringBuilder();
try (BufferedReader r = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "UTF-8"))) {
String line;
while ((line = r.readLine()) != null) {
sb.append(line + "\n");
}
}
return sb.toString();
}

public static Document createDocumentFromString(String xml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
}

static String value;

public static String result() {

try {
String url = "http://opendata.fmi.fi/wfs/fin?service=WFS&version=2.0.0&request=GetFeature&storedquery_id=fmi::observations::mareograph::timevaluepair&fmisid=134223&";

String xml = downloadString(url);
Document document = createDocumentFromString(xml);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceResolver(document));

String time = xpath.evaluate("//wml2:MeasurementTimeseries[@gml:id='obs-obs-1-1-WATLEV']/wml2:point[last()]//wml2:time", document);
value = xpath.evaluate("//wml2:MeasurementTimeseries[@gml:id='obs-obs-1-1-WATLEV']/wml2:point[last()]//wml2:value", document);
System.out.format("time = %s; value = %s\n", time, value);
return value;
} catch (Exception e) {
return "FAIL: " + e.toString();
}
}

}

在 android studio 中运行时输出:"null"并且还抛出 "android.os.NetworkOnMainThreadException"

在 Eclipse 中运行时的输出:"-97.0" (正确输出)

最佳答案

您需要在另一个线程上运行网络相关任务,如下所示:

Thread mThread = new Thread(new Runnable() {
@Override
public void run() {
try {
//Put your code that you want to run in here
} catch (Exception e) {
e.printStackTrace();
}
}

现在,如果您不确定导致此问题的原因,您可以检查错误日志,它会引导您找到导致此问题的行。

您可以做的另一件事是添加带有错误描述的自定义日志/打印,稍后,您可以检查这些日志以查看是否调用了其中的任何一个(如果是,则意味着您收到了错误)

关于java - 如何修复 android.os.NetworkOnMainThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982375/

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