01-09-2012 01:19 AM - edited 01-09-2012 01:22 AM
public class Connection extends MainScreen{
CompanyScreen mainScreen;
HttpConnection connection = null;
InputStream is = null;
OutputStream os = null;
StringBuffer sb = new StringBuffer();
LabelField perusahaan;
public Connection(){
perusahaan = new LabelField();
add(perusahaan);
Conn c = new Conn();
PleaseWaitPopupScreen.showScreenAndWait(c, "Sedang Memuat...");
}
public void dataMasuk(String[] tempat)
{
perusahaan.setText(perusahaan.getText()+tempat);
}
class Conn implements Runnable{
String[] dataComp;
String[] wadah;
public void run(){
try{
net.rim.blackberry.api.browser.URLEncodedPostData encoder = new net.rim.blackberry.api.browser.URLEncodedPostData( null, false);
connection = (HttpConnection) Connector.open("http://ecc.ft.ugm.ac.id/bbapp/vacbycomp.php");
connection.setRequestMethod(HttpConnection.GET); connection.setRequestProperty(net.rim.device.api.i o.http.HttpProtocolConstants.HEADER_CONTENT_TYPE," application /x-www-form-urlencoded");
byte[] postBytes = encoder.toString().getBytes("UTF-8");
connection.setRequestProperty(net.rim.device.api.i o.http.HttpProtocolConstants.HEADER_CONTENT_LENGTH , Integer.toString(postBytes.length));
os = connection.openOutputStream();
os.write(postBytes);
os.flush();
os.close();
if (connection.getResponseCode() == javax.microedition.io.HttpConnection.HTTP_OK) {
is=connection.openInputStream();
String xml = "";
int ch;
while((ch=is.read()) != -1){
sb.append((char)ch);
}
xml = sb.toString();
JSONObject jso = new JSONObject(xml); //response from the server
JSONArray mystring = jso.getJSONArray("company");
JSONArray address = jso.getJSONArray("address");
JSONArray logoComp = jso.getJSONArray("logoComp");
JSONArray desc = jso.getJSONArray("desc");
JSONArray id_comp = jso.getJSONArray("id_comp");
String[] dataComp = new String[mystring.length()];
String[] dataAdd = new String[address.length()];
String[] dataImg = new String[logoComp.length()];
String[] dataDesc = new String[desc.length()];
String[] dataId = new String[id_comp.length()];
for(int i=0;i<mystring.length();i++){
wadah[i]=dataComp[i];
}
}
} catch (java.io.IOException e)
{
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(5000);
}catch (InterruptedException ex){
}
final String[] tempat = wadah;
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
dataMasuk(tempat);
}
});it is my code? if i compile im getting error... whats wrong with my code?
01-09-2012 01:25 AM - edited 01-09-2012 01:25 AM
What Error You are getting compile time, please explain,
I am assuming you are following PleaseWaitPopupScreen described in kb articles, which accept parameter thread, not Runnable, so you would need to make the thread
Conn c = new Conn();
Thread thread=new Thread(c); thread.start();
PleaseWaitPopupScreen.showScreenAndWait(thread, "Sedang Memuat...");
01-09-2012 01:52 AM
Hi jitten... thanks again...
im create pleasewaitscreen like peter_strange sample...
im modify code like ur posting... but still get error...
in first run application result is "null" somtimes get error app... whats the problem?
01-09-2012 02:53 AM - edited 01-09-2012 03:33 AM
Please find the edited source:
Null Pointer you are getting because of arrays are not initialized, and I have edited code as there is no requirement to post parameter in this service.
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import com.bharatberry.container.PleaseWaitPopupScreen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class Connection extends MainScreen{
HttpConnection connection = null;
InputStream is = null;
OutputStream os = null;
StringBuffer sb = new StringBuffer();
LabelField perusahaan;
public Connection(){
perusahaan = new LabelField();
add(perusahaan);
Conn c = new Conn();
Thread thread= new Thread(c);
PleaseWaitPopupScreen.showScreenAndWait(thread, "Sedang Memuat...");
thread.start();
}
public void dataMasuk(String[] tempat)
{
perusahaan.setText(perusahaan.getText()+tempat);
}
class Conn implements Runnable{
String[] dataComp;
String[] wadah;
public void run(){
try{
//net.rim.blackberry.api.browser.URLEncodedPostDat a encoder = new net.rim.blackberry.api.browser.URLEncodedPostData( null, false);
connection = (HttpConnection) Connector.open("http://ecc.ft.ugm.ac.id/bbapp/vacbycomp.php");
connection.setRequestMethod(HttpConnection.GET); connection.setRequestProperty(net.rim.device.api.i o.http.HttpProtocolConstants.HEADER_CONTENT_TYPE," application /x-www-form-urlencoded");
//byte[] postBytes = encoder.toString().getBytes("UTF-8");
//connection.setRequestProperty(net.rim.device.api .io.http.HttpProtocolConstants.HEADER_CONTENT_LENG TH, Integer.toString(postBytes.length));
//os = connection.openOutputStream();
//os.write(postBytes);
//os.flush();
//os.close();
if (connection.getResponseCode() == javax.microedition.io.HttpConnection.HTTP_OK) {
is=connection.openInputStream();
String xml = "";
int ch;
while((ch=is.read()) != -1){
sb.append((char)ch);
}
xml = sb.toString();
JSONObject jso = new JSONObject(xml); //response from the server
JSONArray mystring = jso.getJSONArray("company");
JSONArray address = jso.getJSONArray("address");
JSONArray logoComp = jso.getJSONArray("logoComp");
JSONArray desc = jso.getJSONArray("desc");
JSONArray id_comp = jso.getJSONArray("id_comp");
dataComp = new String[mystring.length()];
wadah = new String[mystring.length()];
String[] dataAdd = new String[address.length()];
String[] dataImg = new String[logoComp.length()];
String[] dataDesc = new String[desc.length()];
String[] dataId = new String[id_comp.length()];
for(int i=0;i<mystring.length();i++){
wadah[i]=dataComp[i];
}
}
} catch (java.io.IOException e)
{
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(5000);
}catch (InterruptedException ex){
}
final String[] tempat = wadah;
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
dataMasuk(tempat);
}
});
}
}
}
01-09-2012 04:01 AM
i have try your code edited on post... but getting error LJava.lang.string...
im cek my php on browser is run and load data JSON normaly...
my situasion is i have manager with loop...
and array data with loop to... this situasion make me confuse create code...
01-09-2012 04:52 AM
Its because, you are not parsing data correctly. I am looking into parsing code, and will post parsing code, as soon finish with it.
01-10-2012 01:10 AM
I have Edited your parsing and add a listview to print companyname from the xml.
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import com.bharatberry.container.PleaseWaitPopupScreen;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
public class Connection extends MainScreen implements ListFieldCallback{
HttpConnection connection = null;
InputStream is = null;
OutputStream os = null;
StringBuffer sb = new StringBuffer();
LabelField perusahaan;
ListField lfData=null;
public InfoStores[] dataComp;
public Connection(){
perusahaan = new LabelField();
add(perusahaan);
Conn c = new Conn();
Thread thread= new Thread(c);
PleaseWaitPopupScreen.showScreenAndWait(thread, "Sedang Memuat...");
thread.start();
dataComp=new InfoStores[0];
lfData=new ListField();
add(lfData);
lfData.setCallback(this);
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
// TODO Auto-generated method stub
InfoStores item=dataComp[index];
graphics.drawText(item.getName(), 0, y);
}
public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return dataComp[index];
}
public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
public void dataMasuk()
{
//perusahaan.setText(perusahaan.getText()+tempat);
lfData.setSize(dataComp.length);
lfData.invalidate();
}
class Conn implements Runnable{
public void run(){
try{
connection = (HttpConnection) Connector.open("http://ecc.ft.ugm.ac.id/bbapp/vacbycomp.php");
connection.setRequestMethod(HttpConnection.GET); connection.setRequestProperty(net.rim.device.api.i o.http.HttpProtocolConstants.HEADER_CONTENT_TYPE," application /x-www-form-urlencoded");
if (connection.getResponseCode() == javax.microedition.io.HttpConnection.HTTP_OK) {
is=connection.openInputStream();
String xml = "";
int ch;
while((ch=is.read()) != -1){
sb.append((char)ch);
}
xml = sb.toString();
JSONObject jso = new JSONObject(xml); //response from the server
JSONArray mystring = jso.getJSONArray("company");
JSONArray address = jso.getJSONArray("address");
JSONArray logoComp = jso.getJSONArray("logoComp");
JSONArray desc = jso.getJSONArray("desc");
JSONArray id_comp = jso.getJSONArray("id_comp");
dataComp = new InfoStores[mystring.length()];
for(int i=0;i<mystring.length();i++)
{
dataComp[i]=new InfoStores();
dataComp[i].setName(mystring.getString(i));
dataComp[i].setId(id_comp.getString(i));
dataComp[i].setDesc(desc.getString(i));
dataComp[i].setLogoUrl(logoComp.getString(i));
dataComp[i].setAddress(address.getString(i));
}
}
} catch (java.io.IOException e)
{
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(5000);
}catch (InterruptedException ex){
}
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
dataMasuk();
}
});
}
}
}
and datastructure class code:
public class InfoStores {
String id="";
String name="";
String address="";
String desc="";
String logoUrl="";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
}
01-10-2012 01:42 AM
oh...my god...wonderfull... u is my best friend in this forum... u know my problem...
i have try your code.... and sucsessfull... now im learn your code and try to other condition... not in labelField... but on some manager... thankkkkkssss fullll.......