本文整理汇总了Java中javax.microedition.io.HttpConnection.close方法的典型用法代码示例。如果您正苦于以下问题:Java HttpConnection.close方法的具体用法?Java HttpConnection.close怎么用?Java HttpConnection.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.microedition.io.HttpConnection
的用法示例。
在下文中一共展示了HttpConnection.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: urlToStream
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
/**
* Reads the content from the specified HTTP URL and returns InputStream
* where the contents are read.
*
* @return InputStream
* @throws IOException
*/
private InputStream urlToStream(String url) throws IOException {
// Open connection to the http url...
HttpConnection connection = (HttpConnection) Connector.open(url);
DataInputStream dataIn = connection.openDataInputStream();
byte[] buffer = new byte[1000];
int read = -1;
// Read the content from url.
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
while ((read = dataIn.read(buffer)) >= 0) {
byteout.write(buffer, 0, read);
}
dataIn.close();
connection.close();
// Fill InputStream to return with content read from the URL.
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteout.toByteArray());
return byteIn;
}
示例2: postCellPosition
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public void postCellPosition(String cellID, String mcc, String mnc, String lac, double latitude, double longitude, long ts) {
try {
final String url
= URL + "?method=celldb.addcell &" + ACCOUNT +
"&mcc=" + mcc +
"&mnc=" + mnc +
"&cellid=" + cellID +
"&lac=" + lac +
"&latitude=" + latitude +
"&longitude=" + longitude +
"&enduserid=" + controller.getLogin();
controller.log(url);
HttpConnection cnx = (HttpConnection) Connector.open(url);
InputStream is = cnx.openInputStream();
StringBuffer b = new StringBuffer();
int car;
while ((car = is.read()) != -1) {
b.append((char) car);
}
is.close();
cnx.close();
} catch (IOException e) {
controller.log(e.getClass().getName() + ": " + e.getMessage());
}
}
示例3: postCellPosition
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public void postCellPosition(String cellID, String mcc, String mnc, String lac, double latitude, double longitude, long ts) {
try {
final String url = URL + "measure/add" + KEY +
"&mcc=" + mcc +
"&mnc=" + mnc +
"&cellid=" + cellID +
"&lac=" + lac +
"&lat=" + latitude +
"&lon=" + longitude;
controller.log(url);
HttpConnection cnx = (HttpConnection) Connector.open(url);
InputStream is = cnx.openInputStream();
StringBuffer b = new StringBuffer();
int car;
while ((car = is.read()) != -1) {
b.append((char) car);
}
is.close();
cnx.close();
} catch (IOException e) {
controller.log(e.getClass().getName() + ": " + e.getMessage());
}
}
示例4: getStream
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public InputStream getStream() throws IOException {
try {
final HttpConnection connection = (HttpConnection)Connector.open(URI);
connection.setRequestMethod(HttpConnection.GET);
InputStream httpStream = connection.openInputStream();
//Buffer our stream, since reading small units at a time from the network
//increases the likelihood of network errors
return new BufferedInputStream(httpStream) {
/* (non-Javadoc)
* @see java.io.InputStream#close()
*/
public void close() throws IOException {
//Some platforms were having issues with the connection close semantics
//where it was supposed to close the connection when the stream was closed,
//so we'll go ahead and move this here.
connection.close();
super.close();
}
};
} catch(SecurityException se) {
if(this.listener != null) {
listener.onSecurityException(se);
}
throw new IOException("Couldn't retrieve data from " + this.getLocalURI() + " due to lack of permissions.");
}
}
示例5: closeConnection
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
private static void closeConnection(HttpConnection connection) {
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例6: readAndClose
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
private static ByteArrayOutputStream readAndClose(HttpConnection connection) throws IOException {
InputStream responseData = connection.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10000];
int bytesRead = responseData.read(buffer);
while (bytesRead > 0) {
baos.write(buffer, 0, bytesRead);
bytesRead = responseData.read(buffer);
}
baos.close();
connection.close();
return baos;
}
示例7: safelyCloseStream
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
private static void safelyCloseStream(HttpConnection stream) {
if (stream != null) {
try {
stream.close();
stream = null;
} catch (Exception e) { /* that's ok */ }
}
}
示例8: getTime
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public long getTime() throws IOException {
HttpConnection conn = (HttpConnection) Connector.open(url);
try {
int rc = conn.getResponseCode();
if (rc == 200) {
BufferedReader reader = new BufferedReader(conn.openInputStream());
return Long.parseLong(reader.readLine());
} else {
throw new RuntimeException("Server returned " + rc);
}
} finally {
conn.close();
}
}
示例9: getTime
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public long getTime() throws IOException {
HttpConnection conn = (HttpConnection) Connector.open(url);
try {
conn.setRequestMethod(method);
long time = conn.getDate();
if (time != 0) {
return time / 1000;
} else {
throw new RuntimeException("Server returned no valid date in http header");
}
} finally {
conn.close();
}
}
示例10: commandWget
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
private void commandWget(String substring) throws IOException {
String[] args = Strings.split(' ', substring);
String url = args[0];
String dest;
if (args.length == 2) {
dest = args[1];
} else {
dest = url.substring(url.lastIndexOf('/'));
}
if (dest.length() == 0) {
dest = "out";
}
FileConnection output = getFile(dest);
if (!output.exists()) {
output.create();
}
OutputStream os = output.openOutputStream();
HttpConnection input = (HttpConnection) Connector.open(url);
InputStream is = input.openInputStream();
copyStream(is, os);
os.close();
output.close();
is.close();
input.close();
}
示例11: urlToProperties
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
protected Properties urlToProperties(String url) throws IOException {
HttpConnection conn = (HttpConnection) Connector.open(url);
conn.setRequestProperty("user-agent", userAgent);
try {
int rc = conn.getResponseCode();
// If we get a wrong HTTP response code, we might as well just stop trying
if (rc != HttpConnection.HTTP_OK) {
if (Logger.BUILD_NOTICE) {
Logger.log(this + ".urlToProperties: Could not fetch " + url + " !", true);
}
cancel();
}
BufferedReader reader = new BufferedReader(conn.openInputStream());
{
String line;
Properties props = new Properties();
while ((line = reader.readLine()) != null) {
int p = line.indexOf(":");
if (p != -1) {
String name = line.substring(0, p);
String value = line.substring(p + 1).trim();
// Logger.log(name + " = " + value);
props.addProperty(name, value);
}
}
return props;
}
} finally {
conn.close();
}
}
示例12: handleResponse
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
private void handleResponse(final HttpConnection connection) throws IOException {
int responseCode = connection.getResponseCode();
responseProperties = HttpRequestProperties.HttpResponsePropertyFactory(connection);
long responseLength = connection.getLength();
if (responseLength > TransportService.PAYLOAD_SIZE_REPORTING_THRESHOLD) {
Logger.log("recv", "size " + responseLength);
}
if(responseCode >= 200 && responseCode < 300) {
//It's all good, message was a success.
this.setResponseCode(responseCode);
this.setStatus(TransportMessageStatus.SENT);
//Wire up the input stream from the connection to the message.
this.setResponseStream(new InputStreamC(connection.openInputStream(), responseLength, this.getTag()) {
/* (non-Javadoc)
* @see java.io.InputStream#close()
*/
public void close() throws IOException {
//Some platforms were having issues with the connection close semantics
//where it was supposed to close the connection when the stream was closed,
//so we'll go ahead and move this here.
connection.close();
super.close();
}
});
} else {
this.setStatus(TransportMessageStatus.FAILED);
this.setResponseCode(responseCode);
//We'll assume that any failures come with a message which is sufficiently
//small that they can be fit into memory.
byte[] response = StreamUtil.readFully(connection.openInputStream());
connection.close();
String reason = responseCode + ": " + new String(response);
reason = PropertyUtils.trim(reason, 400);
this.setFailureReason(reason);
}
}
示例13: run
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public void run() {
// Call to
// /api/0.1/getBugs?b=36.17496&t=61.03797&l=-9.9793&r=31.54902&ucid=1
// Reply is something like: putAJAXMarker(552542, 6.971592, 50.810296,
// 'Strassensystem auf Friedhof fehlt [TobiR, 2010-08-09 23:30:37
// CEST]', 0);
try {
String url = OpenStreetBugs.API_URL + "getBugs?b=" + ymin + "&t=" + ymax + "&l=" + xmin
+ "&r=" + xmax;
HttpConnection httpConn = (HttpConnection) Connector.open(url);
DataInputStream in = httpConn.openDataInputStream();
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int openbrackets = 0;
for (int b = in.read(); b != -1; b = in.read()) {
char c = (char) b;
if (c == ')') {
openbrackets--;
if (openbrackets == 0) {
Bug bug = Bug.parse(buf.toString());
buf.reset();
if (bug != null) {
rec.receiveBug(bug);
if (++receivedBugs >= MAX_BUGS) {
break;
}
}
}
} else if (c == '(') {
openbrackets++;
} else if (openbrackets > 0) { // only store bytes between '('
// ')'
buf.write(b);
}
}
in.close();
httpConn.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}
示例14: run
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
public void run() {
try {
// Visit the original download URL and read the JAD;
// if the MIDlet-Version has changed, invoke the callback.
String url = Build.DOWNLOAD_URL;
String applicationVersion = getApplicationVersion();
String userAgent = getUserAgent();
String language = getLanguage();
for (int redirectCount = 0; redirectCount < 10; redirectCount++) {
HttpConnection c = null;
InputStream s = null;
try {
c = connect(url);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("User-Agent", userAgent);
c.setRequestProperty("Accept-Language", language);
int responseCode = c.getResponseCode();
if (responseCode == HttpConnection.HTTP_MOVED_PERM
|| responseCode == HttpConnection.HTTP_MOVED_TEMP) {
String location = c.getHeaderField("Location");
if (location != null) {
url = location;
continue;
} else {
throw new IOException("Location header missing");
}
} else if (responseCode != HttpConnection.HTTP_OK) {
throw new IOException("Unexpected response code: " + responseCode);
}
s = c.openInputStream();
String enc = getEncoding(c);
Reader reader = new InputStreamReader(s, enc);
final String version = getMIDletVersion(reader);
if (version == null) {
throw new IOException("MIDlet-Version not found");
} else if (!version.equals(applicationVersion)) {
Application application = Application.getApplication();
application.invokeLater(new Runnable() {
public void run() {
mCallback.onUpdate(version);
}
});
} else {
// Already running latest version
}
} finally {
if (s != null) {
s.close();
}
if (c != null) {
c.close();
}
}
}
} catch (Exception e) {
System.out.println(e);
}
}
示例15: getPosition
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public Position getPosition(String cellID, String mcc, String mnc, String lac) {
try {
final String url
= URL + "?method=celldb.getcell&" + ACCOUNT +
"&mcc=" + mcc +
"&mnc=" + mnc +
"&cellid=" + cellID +
"&lac=" + lac +
"&format=csv";
controller.log(url);
HttpConnection cnx = (HttpConnection) Connector.open(url);
InputStream is = cnx.openInputStream();
StringBuffer b = new StringBuffer();
int car;
while ((car = is.read()) != -1) {
b.append((char) car);
}
is.close();
cnx.close();
String res = b.toString();
if (res.endsWith("not valid") || res.endsWith("not found")) {
return null;
} else {
int pos = res.indexOf(',');
pos = res.indexOf(',', pos + 1);
pos = res.indexOf(',', pos + 1);
pos = res.indexOf(',', pos + 1);
//mcc,mnc,lac,cellid,latitude,longitude
int pos2 = res.indexOf(',', pos + 1);
String latitude = res.substring(pos + 1, pos2);
int pos3 = res.indexOf(',', pos2 + 1);
String longitude = res.substring(pos2 + 1, pos3);
return new Position(
Double.parseDouble(longitude),
Double.parseDouble(latitude),
0d,
(short) 0,
0d,
System.currentTimeMillis(),
1000
);
}
} catch (IOException e) {
controller.log(e.getClass().getName() + ": " + e.getMessage());
}
return null;
}