0

I was told to ask this as a new question.

I have no idea how to do what I am doing, and I am just trying to hack something together really quickly.

But here's what I've got:

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.

Here is my function call:

 newScanThing("UPC_A","816983011311");

Anyways, what the heck am I doing wrong? I've never used this before. And I need to get this string for a proof of concept due in about 12 hours, and I have a lot of work to do after I get the string. So tips? Gestures at the right answer?

Maybe just a quick and dirty explanation of what I am doing wrong, how to do it right and why it works that way?

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): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-25 05:53:42.390: D/libEGL(13564): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-25 05:53:42.492: D/OpenGLRenderer(13564): Enabling debug mode 0
01-25 05:53:42.500: E/SensorManager(13564): thread start
01-25 05:53:42.507: D/SensorManager(13564): registerListener :: handle = 1  name= BMA254 Acceleration Sensor delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41816ab8
01-25 05:53:42.593: W/IInputConnectionWrapper(13564): showStatusIcon on inactive InputConnection
01-25 05:53:54.406: D/AndroidRuntime(13564): Shutting down VM
01-25 05:53:54.406: W/dalvikvm(13564): threadid=1: thread exiting with uncaught exception (group=0x4106a2a0)
01-25 05:53:54.421: E/AndroidRuntime(13564): FATAL EXCEPTION: main
01-25 05:53:54.421: E/AndroidRuntime(13564): android.os.NetworkOnMainThreadException
01-25 05:53:54.421: E/AndroidRuntime(13564):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at java.net.URL.openStream(URL.java:462)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at com.example.food2forkbarcode.MainActivity.newScanThing(MainActivity.java:60)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at com.example.food2forkbarcode.MainActivity.onClick(MainActivity.java:28)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at 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):    at android.os.Handler.handleCallback(Handler.java:615)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at android.os.Looper.loop(Looper.java:137)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at android.app.ActivityThread.main(ActivityThread.java:4895)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at java.lang.reflect.Method.invokeNative(Native Method)
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):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
01-25 05:53:54.421: E/AndroidRuntime(13564):    at dalvik.system.NativeStart.main(Native Method)
01-25 05:54:02.820: I/Process(13564): Sending signal. PID: 13564 SIG: 9
01-25 05:54:10.656: D/libEGL(13768): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-25 05:54:10.664: D/libEGL(13768): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-25 05:54:10.671: D/libEGL(13768): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-25 05:54:10.781: D/OpenGLRenderer(13768): Enabling debug mode 0
01-25 05:54:10.789: E/SensorManager(13768): thread start
01-25 05:54:10.789: D/SensorManager(13768): registerListener :: handle = 1  name= BMA254 Acceleration Sensor delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41813160
01-25 05:54:12.187: D/AndroidRuntime(13768): Shutting down VM
01-25 05:54:12.187: W/dalvikvm(13768): threadid=1: thread exiting with uncaught exception (group=0x4106a2a0)
01-25 05:54:12.203: E/AndroidRuntime(13768): FATAL EXCEPTION: main
01-25 05:54:12.203: E/AndroidRuntime(13768): android.os.NetworkOnMainThreadException
01-25 05:54:12.203: E/AndroidRuntime(13768):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at java.net.URL.openStream(URL.java:462)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at com.example.food2forkbarcode.MainActivity.newScanThing(MainActivity.java:60)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at com.example.food2forkbarcode.MainActivity.onClick(MainActivity.java:28)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at android.view.View.performClick(View.java:4222)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at android.view.View$PerformClick.run(View.java:17273)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at android.os.Handler.handleCallback(Handler.java:615)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at android.os.Looper.loop(Looper.java:137)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at android.app.ActivityThread.main(ActivityThread.java:4895)
01-25 05:54:12.203: E/AndroidRuntime(13768):    at java.lang.reflect.Method.invokeNative(Native Method)
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):    at dalvik.system.NativeStart.main(Native Method)
01-25 05:55:03.835: D/dalvikvm(13768): GC_CONCURRENT freed 245K, 9% free 7464K/8199K, paused 6ms+3ms, total 44ms
01-25 05:57:28.851: D/dalvikvm(13768): GC_CONCURRENT freed 332K, 11% free 7572K/8455K, paused 16ms+2ms, total 63ms
Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
CamelopardalisRex
  • 353
  • 1
  • 2
  • 16

1 Answers1

1

The error you get (NetworkOnMainThreadException) means that you are trying to do network operations on the main thread, which is not authorized anymore.

One solution is to execute all the network related operations in an AsyncTask. You have a good example on the link provided by Henry : How to fix android.os.NetworkOnMainThreadException?

Basically, you will create a class that inherits AsyncTask, in the doInBackground method of this class you will do your network operations, and in onPostExecute of the same class you will do the post treatment. You will call this class by creating an new instance and calling .execute().

Community
  • 1
  • 1
jbihan
  • 3,053
  • 2
  • 24
  • 34
  • Ok, I did this, but now my line: formatTxt.setText("FORMAT: " + scanFormat + " DATA: " + scanData); is broken. I think it's because formatTxt is part of my other class, in the other java file. How can I edit another class's stuff? – CamelopardalisRex Jan 25 '14 at 14:55
  • You could declare your `EditText` as a member of your main class, and initialize it like you did before. Then declare your `AsyncTask` in the same file as your main class, as an inner class, and you will have access to your `EditText` – jbihan Jan 25 '14 at 15:03
  • Or you could pass your `EditText` as an argument of your `AsyncTask`. Not sure what is best though – jbihan Jan 25 '14 at 15:05
  • I guess you finally got your answer in the other thread http://stackoverflow.com/questions/21352488/java-edit-textfield-in-another-class ;) – jbihan Jan 25 '14 at 15:59