Wednesday, April 23, 2014

Help using. GetResponseCode () method throws an exception


Reads the network picture, but every time the execution time getResponseCode () method throws an exception, is about to collapse, and in the end is how the code below:
 package com.qxj . lixinxing.service; 

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.qxj.lixinxing.utils.StreamTool;

import android.net.Uri;
import android.util.Log;

public class ImageService {

public static byte [] getImage (String path) throws Exception {

URL url = new URL (path); / / get the resources to address
HttpURLConnection connection = (HttpURLConnection) url.openConnection (); / / get a connection
connection.disconnect ();
connection.setConnectTimeout (5000); / / if not connected to the 5000 milliseconds timeout
connection.setRequestMethod ("GET"); / / to "get" the way a request
When / / every execution to determine the following statement will throw an exception, is how is it?
if (connection.getResponseCode () == 200) {/ / if the data exists, the next step
InputStream inStream = connection.getInputStream (); / / get the input stream returned
Networkreturn StreamTool.read (inStream); / / input stream into binary data
}
return null;
}

}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
The judge sentences commented, directly input stream will throw an exception, I add a network access permissions in the configuration file inside ah
Reply:
There is no one to help see how it
Reply:
The exception also sent to ah
Reply:
networkonmainthreadexception
Reply:
Clearly the operation of the network to operate on a thread, the error is obvious to tell you that you put on the main thread of the network operator which is not allowed! You need to start a thread for network operation
new thread () {

public void run () {
Write your code to try those!
}

}. Start ()
Reply:
connection.disconnect ();
This is why you want to disconnect?
Reply:
reference to the 6th floor longer262110 reply:
connection.disconnect ();
This is why you want to disconnect?

The amount is, why do you want to disconnect
Reply:
Identify problems, and calls MainActivity in network operation method of this class may cause some problems activity, and Google android2.3 from later versions, the system adds a class: StrictMode. This class of network access methods, which must be changed.
StrictMode typically used to capture disk access or network access issues in the primary process and the interaction between the generated because the main process, the implementation of UI operations and some action is the most frequently used, would produce a certain conflict between them . The disk access and network access from the main thread stripping can make a disk or network access smoother, improve responsiveness and user experience.
If you must, then this operation, you need to add the following code in MainActivity in:
StrictMode.setThreadPolicy (new
. StrictMode.ThreadPolicy.Builder () detectDiskReads () detectDiskWrites () detectNetwork () penaltyLog () build ());....
StrictMode.setVmPolicy (
. new StrictMode.VmPolicy.Builder () detectLeakedSqlLiteObjects () detectLeakedClosableObjects () penaltyLog () penaltyDeath () build ());....

And configuration files in the SDK in the lowest level to 11
Reply:
reference to the 8th floor xinxing260 reply:
identify problems, and to call the network operating method of this class in MainActivity, it may cause some problems activity of Google android2.3 from later versions, the system adds a class: StrictMode. This class of network access methods, which must be changed.
StrictMode typically used to capture disk access or network access issues in the primary process and the interaction between the generated because the main process, the implementation of UI operations and some action is the most frequently used, would produce a certain conflict between them . The ......

There is no wrong? connection.disconnect ();
Reply:
Useful, I also encountered the same problem
Reply:

Reply:
11th Floor End Citie
Reply:
Wrong, or starting a method of 8l

No comments:

Post a Comment