我正在努力让黑莓获得千禧媒体的广告请求,我希望得到一些帮助,我似乎无法让它发挥作用。这就是我所拥有的:
String url ="http://ads.mp.mydas.mobi/getAd?apid=157899&auid=ddd25abbb993f79454b12827c803fbafab2ad89b";
try {
conn = (HttpConnection) new ConnectionFactory().getConnection(url).getConnection(); conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
in = conn.openInputStream();
// parser.parse(in, handler);
buff.append(IOUtilities.streamToBytes(in));
result = buff.toString();
}
else {
result = "Error in connection" + conn.getResponseCode();
((ButterflyScreen)screen).update(result);
}
catch (Exception ex) {ex.printStackTrace();
}
finally {
try{ if (in !=null)
{in.close();
}
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
以下是我尝试实现get请求的类的完整代码。
import java.io.*;
import java.rmi.*;
import javax.microedition.io.HttpConnection;
import net.rim.blackberry.api.browser.Browser;
import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.io.transport.ConnectionFactory;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.BorderFactory;
import com.ctpl.butterfly.custom.HrefField;
/**
* A class extending the MainScreen class,This is Home Screen on App.
*/
public final class ButterflyScreen extends MainScreen {
private ButtonField btnSetting;
private LabelField lblMsgNameOfApp, lblMsgDesc, lblHeading;
private HrefField lblMsgWebLink;
private Bitmap borderBitmap;
ButterflyScreen screen;
HttpConnection conn = null;
InputStream in = null;
StringBuffer buff = new StringBuffer();
String result = "";
/**
* Creates a new ButterflyScreen object
*/
public ButterflyScreen() {
super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
this.getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(0x0099CCFF,
0x0099CCFF,0x00336699,0x00336699)
);
lblHeading = new LabelField(StringRes.APP_NAME) {
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
HorizontalFieldManager topPannel = new HorizontalFieldManager(
Field.FIELD_HCENTER | Field.USE_ALL_WIDTH) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
int x = getWidth() / 2;
x = x - lblHeading.getWidth() / 2;
int y = 0;
setPositionChild(lblHeading, x, y);
setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("footer_back.png")));
}
};
lblHeading.setFont(lblHeading.getFont().derive(Font.BOLD));
topPannel.add(lblHeading);
setTitle(topPannel);
lblMsgNameOfApp = new LabelField(StringRes.WORLD+" \n") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.PURPLE);
super.paint(graphics);
}
};
lblMsgDesc = new LabelField(StringRes.WELCOME_SCREEN_MESSAGE+" \n");
lblMsgWebLink = new HrefField("© Carib Tech Production Limited");
lblMsgWebLink.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
Browser.getDefaultSession().displayPage("http://caribtechprod.com");
}
});
btnSetting = new ButtonField(StringRes.SETTING_SCREEN);
btnSetting.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
UiApplication.getUiApplication().pushScreen(new SettingWaitScreen());
}
});
setDescScreen();
}
private void setDescScreen() {
VerticalFieldManager vfmForCenterDisplay = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT | Field.FIELD_HCENTER | Field.FIELD_VCENTER ) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
int topSpace = (Display.getHeight() / 8);
setPadding(topSpace, 0, 0, 0);
}
};
vfmForCenterDisplay.setBackground(BackgroundFactory.createLinearGradientBackground(0x0099CCFF,
0x0099CCFF,0x00336699,0x00336699)
);
// Please verify this image src
borderBitmap = Bitmap.getBitmapResource("img/rounded-border.png");
VerticalFieldManager vfmBasicInfoWithBorder = new VerticalFieldManager() {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
Background background = BackgroundFactory.createSolidBackground(Color.WHITE);
setBackground(background);
}
};
vfmBasicInfoWithBorder.setPadding(0, 0, 5, 5);
vfmBasicInfoWithBorder.add(lblMsgNameOfApp);
vfmBasicInfoWithBorder.add(new LabelField());
vfmBasicInfoWithBorder.add(lblMsgDesc);
vfmBasicInfoWithBorder.add(new LabelField());
vfmBasicInfoWithBorder.add(lblMsgWebLink);
HorizontalFieldManager hfm1 = new HorizontalFieldManager(Field.FIELD_HCENTER);
hfm1.add(btnSetting);
vfmBasicInfoWithBorder.setBorder(BorderFactory.createBitmapBorder(new XYEdges(12, 12, 12, 12), borderBitmap));
***String url ="http://ads.mp.mydas.mobi/getAd?apid=157899&auid=ddd25abbb993f79454b12827c803fbafab2ad89b";
try {
conn = (HttpConnection) new ConnectionFactory().getConnection(url).getConnection(); conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
in = conn.openInputStream();
// parser.parse(in, handler);
buff.append(IOUtilities.streamToBytes(in));
result = buff.toString();
}
else {
result = "Error in connection" + conn.getResponseCode();
((ButterflyScreen)screen).update(result);
}
catch (Exception ex) {ex.printStackTrace();
}
finally {
try{ if (in !=null)
{in.close();
}
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}***
VerticalFieldManager verticalFieldManager = new VerticalFieldManager(Field.FIELD_HCENTER|Field.FIELD_VCENTER);
add(verticalFieldManager);
VerticalFieldManager vfm = new VerticalFieldManager
(VerticalFieldManager.NO_VERTICAL_SCROLL
| VerticalFieldManager.NO_VERTICAL_SCROLLBAR
| VerticalFieldManager.USE_ALL_WIDTH);
HorizontalFieldManager hfm = new HorizontalFieldManager
(HorizontalFieldManager.FIELD_HCENTER
| HorizontalFieldManager.FIELD_VCENTER);
hfm.add(verticalFieldManager);
vfm.add(hfm);
add(vfm);
//vfmForCenterDisplay.add(new ButtonField("test"));
vfmForCenterDisplay.add(vfmBasicInfoWithBorder);
vfmForCenterDisplay.add(hfm1);
add(vfmForCenterDisplay);
}
**private void update(String string) {
// TODO Auto-generated method stub
VerticalFieldManager verticalFieldManager = new VerticalFieldManager(Field.FIELD_HCENTER|Field.FIELD_VCENTER);{
VerticalFieldManager vfm = new VerticalFieldManager
(VerticalFieldManager.NO_VERTICAL_SCROLL
| VerticalFieldManager.NO_VERTICAL_SCROLLBAR
| VerticalFieldManager.USE_ALL_WIDTH);
HorizontalFieldManager hfm = new HorizontalFieldManager
(HorizontalFieldManager.FIELD_HCENTER
| HorizontalFieldManager.FIELD_VCENTER);
hfm.add(verticalFieldManager);
vfm.add(hfm);
add(vfm);
}
}**
public boolean onClose()
{
setDirty(false);
return true;
}
}
我把***放在不确定的地方,我需要你的帮助。