1. We have to take all the keys and values in One Hash table or Array of Hash tables(Vector)
2. Convert it into JSON object.
3. Send to server.
package
com.alishaik.thread;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.util.Hashtable;
import
javax.microedition.io.Connector;
import
javax.microedition.io.HttpConnection;
import
net.rim.blackberry.api.browser.URLEncodedPostData;
import
net.rim.device.api.io.http.HttpProtocolConstants;
import
net.rim.device.api.ui.Screen;
import
net.rim.device.api.ui.UiApplication;
import
com.hiddenbrains.ddft.common.CommonMethods;
import
com.hiddenbrains.ddft.common.MessageScreen;
import
com.hiddenbrains.ddft.common.Utility;
import
com.hiddenbrains.ddft.json.JSONObject;
public
class
PostDataThread
extends
Thread
{
private
HttpConnection httpConnection;
private
InputStream inputStream;
private
StringBuffer stringBuffer=
new
StringBuffer();
private
Hashtable hashTable;
private
String URL=
""
;
private
int
index=
0
;
public
PostDataThread(String url, Hashtable hash,
int
index)
{
// System.out.println("=========WEB SERVICE===========: "+url);
URL=Utility.escapeHTML(url)+CommonMethods.updateConnectionSuffix();//interface=wifi; or deviceside=false;
this
.index=index;
this
.hashTable=hash;//Here I am taking one Hashtable. You can manage according to your requirement.
}
public
void
run()
{
try
{
httpConnection=(HttpConnection)Connector.open(URL);
httpConnection.setRequestMethod(HttpConnection.POST);
URLEncodedPostData oPostData =
new
URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET,
false
);
oPostData.setData(provideJSONObject());//IMPORTANT
byte
[] postBytes = oPostData.getBytes();
httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
"application / requestJson"
);
httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
OutputStream strmOut = httpConnection.openOutputStream();
strmOut.write(postBytes);
strmOut.flush();
strmOut.close();
int
response=httpConnection.getResponseCode();
if
(response==HttpConnection.HTTP_OK)
{
inputStream = httpConnection.openInputStream();
int
c;
while
((c=inputStream.read())!=-
1
)
{
stringBuffer.append((
char
)c);
}
parseData(stringBuffer.toString());
}
else
parserInterface.didFailParser(
"Connection failure HTTP Response: "
+response);
}
catch
(
final
Exception e)
{
String msg=e.getMessage();
if
(msg==
null
)
msg="No Data Found";
//Show failure alert here
}
finally
{
try
{
if
(httpConnection!=
null
)
httpConnection.close();
if
(inputStream!=
null
)
inputStream.close();
}
catch
(Exception e2)
{}
}
}
private
String provideJSONObject()
{
try
{
JSONObject jsonObj=
new
JSONObject();
jsonObj.put(
"data"
, hashTable);
//Getting correct data; You can check here
// System.out.println("======================JSON STRING FOR POSTING: "+jsonObj.toString());
return
jsonObj.toString();
}
catch
(Exception e)
{
return
null
;
}
}
private
void
parseData(String responseString)
{
responseString=
"["
+responseString+
"]"
;
System.out.println("======================\n\n"+responseString+"\n\n===================");
//Do what you want.
}
}