gpt4 book ai didi

java - Java URL getContent Android

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

我不知道该怎么做,而我只是想很快地将某些东西弄混。

但是,这就是我所拥有的:

public void newScanThing(String scanFormat, String scanContent){
URL u;
try {
u = new URL("http://www.searchupc.com/handlers/upcsearch.ashx?request_type=3&access_token=A98AC4EC-830D-4F6B-89D6-D6954361A8CA&upc=" + scanContent.toString());
BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream()));
String stringBuff;
String scanData = "";
while ((stringBuff = br.readLine()) != null){
scanData += stringBuff;

}
br.close();

formatTxt.setText("FORMAT: " + scanFormat + " DATA: I don't crash.");
} catch (MalformedURLException mue) {
// crash
} catch (IOException ioe) {
// crash
}I know which line causes it to crash, and it is the scanData = new ... line. But I have no idea why. If you were to visit the link I visit, you would see the only thing in that URL is a single string. It's not even any HTML or anything, it's just a string.


这是我的函数调用:

 newScanThing("UPC_A","816983011311");


无论如何,我到底在做什么错?我以前从未用过。而且我需要获得此字符串以在大约12个小时内得到概念证明,在获得字符串之后,我还有很多工作要做。那么提示?正确答案的手势?

也许只是对我做错了什么,如何正确做以及为什么那样做的快速而肮脏的解释?

LogCat:

01-25 05:53:42.367: D/libEGL(13564): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so


01-25 05:53:42.382:D / libEGL(13564):已加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-25 05:53:42.390:D / libEGL(13564):已加载/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-25 05:53:42.492:D / OpenGLRenderer(13564):启用调试模式0
01-25 05:53:42.500:E / SensorManager(13564):线程启动
01-25 05:53:42.507:D / SensorManager(13564):registerListener ::句柄= 1名称= BMA254加速传感器延迟= 200000侦听器= android.view.OrientationEventListener$SensorEventListenerImpl@41816ab8
01-25 05:53:42.593:W / IInputConnectionWrapper(13564):无效输入连接上的showStatusIcon
01-25 05:53:54.406:D / AndroidRuntime(13564):关闭VM
01-25 05:53:54.406:W / dalvikvm(13564):threadid = 1:线程以未捕获的异常退出(group = 0x4106a2a0)
01-25 05:53:54.421:E / AndroidRuntime(13564):致命异常:主要
01-25 05:53:54.421:E / AndroidRuntime(13564):android.os.NetworkOnMainThreadException
01-25 05:53:54.421:E / AndroidRuntime(13564):位于android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于java.net.InetAddress.getAllByName(InetAddress.java:214)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpConnection。(HttpConnection.java:70)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpConnection。(HttpConnection.java:50)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于java.net.URL.openStream(URL.java:462)
01-25 05:53:54.421:E / AndroidRuntime(13564):在com.example.food2forkbarcode.MainActivity.newScanThing(MainActivity.java:60)
01-25 05:53:54.421:E / AndroidRuntime(13564):在com.example.food2forkbarcode.MainActivity.onClick(MainActivity.java:28)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于android.view.View.performClick(View.java:4222)
01-25 05:53:54.421:E / AndroidRuntime(13564):at android.view.View $ PerformClick.run(View.java:17273)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于android.os.Handler.handleCallback(Handler.java:615)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于android.os.Handler.dispatchMessage(Handler.java:92)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于android.os.Looper.loop(Looper.java:137)
01-25 05:53:54.421:E / AndroidRuntime(13564):位于android.app.ActivityThread.main(ActivityThread.java:4895)
01-25 05:53:54.421:E / AndroidRuntime(13564):at java.lang.reflect.Method.invokeNative(本机方法)
01-25 05:53:54.421:E / AndroidRuntime(13564):at java.lang.reflect.Method.invoke(Method.java:511)
01-25 05:53:54.421:E / AndroidRuntime(13564):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:994)
01-25 05:53:54.421:E / AndroidRuntime(13564):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
01-25 05:53:54.421:E / AndroidRuntime(13564):在dalvik.system.NativeStart.main(本机方法)
01-25 05:54:02.820:I / Process(13564):发送信号PID:13564 SIG:9
01-25 05:54:10.656:D / libEGL(13768):已加载/vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-25 05:54:10.664:D / libEGL(13768):已加载/vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-25 05:54:10.671:D / libEGL(13768):已加载/vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-25 05:54:10.781:D / OpenGLRenderer(13768):启用调试模式0
01-25 05:54:10.789:E / SensorManager(13768):线程启动
01-25 05:54:10.789:D / SensorManager(13768):registerListener ::句柄= 1名称= BMA254加速传感器延迟= 200000侦听器= android.view.OrientationEventListener$SensorEventListenerImpl@41813160
01-25 05:54:12.187:D / AndroidRuntime(13768):关闭VM
01-25 05:54:12.187:W / dalvikvm(13768):threadid = 1:线程以未捕获的异常退出(group = 0x4106a2a0)
01-25 05:54:12.203:E / AndroidRuntime(13768):致命异常:主要
01-25 05:54:12.203:E / AndroidRuntime(13768):android.os.NetworkOnMainThreadException
01-25 05:54:12.203:E / AndroidRuntime(13768):位于android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于java.net.InetAddress.getAllByName(InetAddress.java:214)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpConnection。(HttpConnection.java:70)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpConnection。(HttpConnection.java:50)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于java.net.URL.openStream(URL.java:462)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于com.example.food2forkbarcode.MainActivity.newScanThing(MainActivity.java:60)
01-25 05:54:12.203:E / AndroidRuntime(13768):在com.example.food2forkbarcode.MainActivity.onClick(MainActivity.java:28)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于android.view.View.performClick(View.java:4222)
01-25 05:54:12.203:E / AndroidRuntime(13768):在android.view.View $ PerformClick.run(View.java:17273)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于android.os.Handler.handleCallback(Handler.java:615)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于android.os.Handler.dispatchMessage(Handler.java:92)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于android.os.Looper.loop(Looper.java:137)
01-25 05:54:12.203:E / AndroidRuntime(13768):位于android.app.ActivityThread.main(ActivityThread.java:4895)
01-25 05:54:12.203:E / AndroidRuntime(13768):at java.lang.reflect.Method.invokeNative(本机方法)
01-25 05:54:12.203:E / AndroidRuntime(13768):at java.lang.reflect.Method.invoke(Method.java:511)
01-25 05:54:12.203:E / AndroidRuntime(13768):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:994)
01-25 05:54:12.203:E / AndroidRuntime(13768):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
01-25 05:54:12.203:E / AndroidRuntime(13768):在dalvik.system.NativeStart.main(本机方法)
01-25 05:55:03.835:D / dalvikvm(13768):GC_CONCURRENT释放245K,释放9%释放7464K / 8199K,暂停6ms + 3ms,总计44ms
01-25 05:57:28.851:D / dalvikvm(13768):GC_CONCURRENT释放了332K,11%释放了7572K / 8455K,暂停了16ms + 2ms,总计63ms

最佳答案

您的代码中实际上存在多个问题:


您将在此行中将InputStream转换为String:

scanData = new String(u.getContent().toString());


这当然不是您想要的。

InputStream为您提供了获取数据的方法,但它并不代表数据本身。您必须在初始化任何String之前从InputStream中读取字节。
您正在主线程上进行联网操作。问题在于网络操作需要时间,如果您在UI线程上执行网络操作,则会阻止UI,应用程序将无法响应。因此,系统禁止在UI线程上执行联网活动,并导致 android.os.NetworkOnMainThreadException崩溃。您必须将网络代码移到另一个线程。为此,我建议使用 AsyncTask


看看这样的网络教程: http://android-er.blogspot.co.at/2012/04/androidosnetworkonmainthreadexception.html

关于java - Java URL getContent Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21350812/

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