您必须使用HTTPConnetion
将所有连接参数添加到您的URL中。创建一个自定义类来检查连接参数。使用以下。
public class HttpConnectionImpl
extends impl.javame.com.twitterapime.io.HttpConnectionImpl {
private static String appendConnectionParameters;
private static String connectionParameters;
private static ServiceRecord getWAP2ServiceRecord() {
String cid;
String uid;
ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] records = sb.getRecords();
//
for (int i = records.length -1; i >= 0; i--) {
cid = records[i].getCid().toLowerCase();
uid = records[i].getUid().toLowerCase();
//
if (cid.indexOf("wptcp") != -1
&& uid.indexOf("wifi") == -1
&& uid.indexOf("mms") == -1) {
return records[i];
}
}
//
return null;
}
public static String getConnectionParams() {
String connParams = "";
//
if (connectionParameters != null) {
connParams = connectionParameters;
} else {
if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
connParams = ";interface=wifi"; //Connected to a WiFi access point.
} else {
int coverageStatus = CoverageInfo.getCoverageStatus();
//
if ((coverageStatus & CoverageInfo.COVERAGE_BIS_B) == CoverageInfo.COVERAGE_BIS_B) {
connParams = ";deviceside=false;ConnectionType=mds-public";
} else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
// Have network coverage and a WAP 2.0 service book record
ServiceRecord record = getWAP2ServiceRecord();
//
if (record != null) {
connParams = ";deviceside=true;ConnectionUID=" + record.getUid();
} else {
connParams = ";deviceside=true";
}
} else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
// Have an MDS service book and network coverage
connParams = ";deviceside=false";
}
}
//
if (appendConnectionParameters != null) {
connParams += appendConnectionParameters;
}
}
//
return connParams;
}
public static void setAppendConnectionParameters(String params) {
if (params != null && !params.startsWith(";")) {
params = ";" + params;
}
//
appendConnectionParameters = params;
}
public static void setConnectionParameters(String params) {
if (params != null && !params.startsWith(";")) {
params = ";" + params;
}
//
connectionParameters = params;
}
public void open(String url) throws IOException {
super.open(url + getConnectionParams());
}
}
以上级合并后u必须作出一个包名称impl.javame.com.twitterapime.io
并添加下面的类。
`package impl.javame.com.twitterapime.io; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.Connector; import com.twitterapime.io.HttpConnection;
public class HttpConnectionImpl implements HttpConnection private javax.microedition.io.HttpConnection httpConn;
public void open(String url) throws IOException {
httpConn =
(javax.microedition.io.HttpConnection)
Connector.open(url, Connector.READ_WRITE, true);
}
public void close() throws IOException {
httpConn.close();
}
public int getResponseCode() throws IOException {
return httpConn.getResponseCode();
}
public InputStream openInputStream() throws IOException {
return httpConn.openInputStream();
}
public OutputStream openOutputStream() throws IOException {
return httpConn.openOutputStream();
}
public void setRequestMethod(String method) throws IOException {
httpConn.setRequestMethod(method);
}
public void setRequestProperty(String key, String value) throws IOException{
httpConn.setRequestProperty(key, value);
}
public String getRequestProperty(String key) throws IOException {
return httpConn.getRequestProperty(key);
}
public String getHeaderField(String name) throws IOException {
return httpConn.getHeaderField(name);
}
}`
现在你必须整合另外两个类
- 的HttpConnection
- HttpResponsein
com.twitterapime.io
包。
`package com.twitterapime.io; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public interface HttpConnection public static final String GET =“GET”;
public static final String POST = "POST";
public static final String HEAD = "HEAD";
public static final int HTTP_OK = 200;
public static final int HTTP_FORBIDDEN = 403;
public static final int HTTP_UNAVAILABLE = 503;
public static final int HTTP_NOT_MODIFIED = 304;
public static final int HTTP_BAD_REQUEST = 400;
public static final int HTTP_UNAUTHORIZED = 401;
public static final int HTTP_NOT_FOUND = 404;
public static final int HTTP_NOT_ACCEPTABLE = 406;
public static final int HTTP_INTERNAL_ERROR = 500;
public static final int HTTP_BAD_GATEWAY = 502;
public void open(String url) throws IOException;
public void close() throws IOException;
public int getResponseCode() throws IOException;
public InputStream openInputStream() throws IOException;
public OutputStream openOutputStream() throws IOException;
public void setRequestMethod(String method) throws IOException;
public void setRequestProperty(String key, String value) throws IOException;
public String getRequestProperty(String key) throws IOException;
public String getHeaderField(String name) throws IOException;
Bwlow是的HttpResponse Class`
`包com.twitterapime。IO;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException;
public final class HttpResponse私有int代码;
private String body;
private InputStream stream;
private HttpConnection conn;
HttpResponse(HttpConnection conn) throws IOException {
this.conn = conn;
code = conn.getResponseCode();
stream = conn.openInputStream();
}
public boolean wasSuccessful() {
return code >= 200 && code < 400;
}
public String getBodyContent() throws IOException {
return body != null ? body : (body = parseBody(stream));
}
public InputStream getStream() {
return stream;
}
public int getCode() {
return code;
}
public String getResponseField(String key) throws IOException {
return conn.getRequestProperty(key);
}
private String parseBody(InputStream in) throws IOException {
if (in == null) {
return null;
}
//
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
//
for (int n; (n = in.read(buffer)) > 0;) {
out.write(buffer, 0, n);
}
//
try {
return new String(out.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IOException(e.getMessage());
}
}
}`
使用下面马托检查您的引黄参数。此方法会自动检查您的设备连接可用性并根据您的连接添加连接参数。
protected HttpConnection getConnection(String url) throws IOException {
url += HttpConnectionImpl.getConnectionParams();
//
return (HttpConnection)Connector.open(url);
}
以上HttpConnection
方法的返回URL与引黄参数..比ü可以使用这个引黄在YOUT InputStream
,打开任何URL以及web服务。
我希望这会帮助你...
您是否检查Device中的连接..?如果可以的话。首先检查设备的默认浏览器,以提供诸如“http://www.google.com”之类的内容。如果浏览器不工作,那就是你的DEVICE问题。 – alishaik786 2012-01-13 12:24:38
设备连接到WIFI,我可以打开谷歌,Facebook和任何网站。顺便说一句,我想我正在听到一条线索。好像我需要在Web服务URL之后放置参数like; deviceside = false。 (但我还没有尝试过。) – NandNand 2012-01-13 14:44:11