Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Trusted Contributor
jaff
Posts: 115
Registered: ‎11-16-2011
My Carrier: 3

Get error when create connection on thread?

[ Edited ]
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.io.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_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?

Please use plain text.
Developer
jitendrasharma
Posts: 208
Registered: ‎08-04-2009

Re: Get error when create connection on thread?

[ Edited ]

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...");
Please use plain text.
Trusted Contributor
jaff
Posts: 115
Registered: ‎11-16-2011
My Carrier: 3

Re: Get error when create connection on thread?

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?

Please use plain text.
Developer
jitendrasharma
Posts: 208
Registered: ‎08-04-2009

Re: Get error when create connection on thread?

[ Edited ]

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.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.io.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_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");


					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);
				}
			});
		}
	}
}

 

Please use plain text.
Trusted Contributor
jaff
Posts: 115
Registered: ‎11-16-2011
My Carrier: 3

Re: Get error when create connection on thread?

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...

 

Please use plain text.
Developer
jitendrasharma
Posts: 208
Registered: ‎08-04-2009

Re: Get error when create connection on thread?

Its because, you are not parsing data correctly. I am looking into parsing code, and will post parsing code, as soon finish with it.

Please use plain text.
Developer
jitendrasharma
Posts: 208
Registered: ‎08-04-2009

Re: Get error when create connection on thread?

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.io.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;
}
}

 

Please use plain text.
Trusted Contributor
jaff
Posts: 115
Registered: ‎11-16-2011
My Carrier: 3

Re: Get error when create connection on thread?

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.......

Please use plain text.