本文整理汇总了Java中javax.microedition.io.HttpConnection.openInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java HttpConnection.openInputStream方法的具体用法?Java HttpConnection.openInputStream怎么用?Java HttpConnection.openInputStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.microedition.io.HttpConnection
的用法示例。
在下文中一共展示了HttpConnection.openInputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getInputStream
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
protected InputStream getInputStream(Object context) throws IOException {
HttpConnection conn = (HttpConnection) context;
int i = 1;
String key = null;
Vector cookieList = new Vector();
while((key=conn.getHeaderFieldKey(i)) != null) {
if (key.toLowerCase().equals("set-cookie") ||
key.toLowerCase().equals("set-cookie2")) {
cookieList.addElement(conn.getHeaderField(i));
}
i++;
}
cookieManager.setCookie(cookieList, conn.getHost());
InputStream istream = conn.openInputStream();
return istream;
}
示例2: getInputStream
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
protected InputStream getInputStream(Object context) throws IOException {
HttpConnection conn = (HttpConnection) context;
int i = 1;
String key = null;
ArrayList cookieList = new ArrayList();
while((key=conn.getHeaderFieldKey(i)) != null) {
if (key.equalsIgnoreCase("set-cookie") ||
key.equalsIgnoreCase("set-cookie2")) {
cookieList.add(conn.getHeaderField(i));
}
i++;
}
cookieManager.setCookie(cookieList, conn.getHost());
InputStream istream = conn.openInputStream();
return istream;
}
示例3: getResponseAsSting
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
private static String getResponseAsSting(HttpConnection conn) throws IOException {
String result = "";
InputStream inputStream = null;
try {
inputStream = conn.openInputStream();
int len;
byte[] data = new byte[512];
do {
len = inputStream.read(data);
if(len > 0){
result += new String(data, 0, len);
}
} while(len > 0);
} catch (Exception e) {
if (e instanceof IOException) {
throw (IOException)e;
}
} finally {
safelyCloseStream(inputStream);
}
return result;
}
示例4: 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());
}
}
示例5: 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());
}
}
示例6: 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.");
}
}
示例7: 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;
}
示例8: setURL
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public void setURL(String host, String url) throws IOException {
closeStream();
conn = (HttpConnection) Connector.open("http://" + host + url, Connector.READ, true);
conn.setRequestMethod(HttpConnection.GET);
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
in = conn.openInputStream();
// Retrieve MIME type of the stream
contentType = conn.getHeaderField("Content-Type");
} else {
throw new IOException("Unexpected HTTP return: " + conn.getResponseCode());
}
}
示例9: 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();
}
}
示例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: 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);
}
}
示例13: 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;
}
示例14: getPosition
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public Position getPosition(String cellID, String mcc, String mnc, String lac) {
try {
final String url = URL + "cell/get" + KEY +
"&mcc=" + mcc +
"&mnc=" + mnc +
"&cellid=" + cellID +
"&lac=" + lac +
"&fmt=txt";
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.startsWith("err")) {
return null;
} else {
int pos = res.indexOf(',');
String latitude = res.substring(0, pos);
int pos2 = res.indexOf(',', pos + 1);
String longitude = res.substring(pos + 1, pos2);
String range = res.substring(pos2 + 1, res.length());
return new Position(
Double.parseDouble(longitude),
Double.parseDouble(latitude),
0d,
(short) 0,
0d,
System.currentTimeMillis(),
Integer.parseInt(range)
);
}
} catch (IOException e) {
controller.log(e.getClass().getName() + ": " + e.getMessage());
}
return null;
}
示例15: flush
import javax.microedition.io.HttpConnection; //导入方法依赖的package包/类
public void flush() throws TTransportException {
// Extract request and reset buffer
byte[] data = requestBuffer_.toByteArray();
requestBuffer_.reset();
try {
// Create connection object
HttpConnection connection = (HttpConnection)Connector.open(url_);
// Timeouts, only if explicitly set
if (connectTimeout_ > 0) {
// XXX: not available
// connection.setConnectTimeout(connectTimeout_);
}
if (readTimeout_ > 0) {
// XXX: not available
// connection.setReadTimeout(readTimeout_);
}
// Make the request
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-thrift");
connection.setRequestProperty("Accept", "application/x-thrift");
connection.setRequestProperty("User-Agent", "JavaME/THttpClient");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Keep-Alive", "5000");
connection.setRequestProperty("Http-version", "HTTP/1.1");
connection.setRequestProperty("Cache-Control", "no-transform");
if (customHeaders_ != null) {
for (Enumeration e = customHeaders_.keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String value = (String)customHeaders_.get(key);
connection.setRequestProperty(key, value);
}
}
// connection.setDoOutput(true);
// connection.connect();
OutputStream os = connection.openOutputStream();
os.write(data);
os.close();
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
throw new TTransportException("HTTP Response code: " + responseCode);
}
// Read the responses
inputStream_ = connection.openInputStream();
} catch (IOException iox) {
System.out.println(iox.toString());
throw new TTransportException(iox);
}
}