Hi Frnds....
I am working on Http connection.Now problem is that I have to send a
JSON object via Http post.Please check my code:[code]
public void setValue()
{
Map<String, String> variables = new HashMap<String, String>();
/* and put the Default-Encoding for html-forms to it. */
variables.put("json-request-id ", "insuranceIDCardRequest");
variables.put("json-request-string", json.toString());
go(variables);
}
public void go(Map<String,String> variables){
/* Create a new EventHandler defined above, to handle what
gets returned. */
MyEventHandler myEvH = new MyEventHandler(this);
/* Create a new HTTP-RequestQueue. */
android.net.http.RequestQueue rQueue = new
RequestQueue(this);
/* Create a header-hashmap */
Map<String, String> headers = new HashMap<String, String>();
/* and put the Default-Encoding for html-forms to it. */
headers.put("Content-Type", "application/x-www-form-
urlencoded");
/* Prepare the variables we are going to send. */
String variablesURL;
if(variables != null && variables.size() > 0){
try {
StringBuilder b = new StringBuilder();
Set<String> keys = variables.keySet();
// Iterate over each variable you want and append it
to our variablesURL variable
for(Iterator<String> i = keys.iterator();;){
String key = i.next();
b.append(key);
b.append("=");
b.append(URLEncoder.encode(variables.get(key),
"UTF-8"));
// Minor speed optimization so that i.hasNext()
is only called once (instead of in the for loop).
if(i.hasNext()){
b.append("&");
} else {
break;
}
}
variablesURL = b.toString();
} catch (UnsupportedEncodingException e) {
Log.e("HTTP_Request", "Unsupported Encoding
Exception");
return;
}
} else {
variablesURL = "";
}
/* And put the encoded bytes into an BAIS,
* where a function later can read bytes from. */
byte[] POSTbytes = variablesURL.getBytes();
ByteArrayInputStream baos = new
ByteArrayInputStream(POSTbytes);
rQueue.queueRequest("http://www.zinsure.net/zinsure/
JSONServlet", "POST", headers, myEvH, baos, POSTbytes.length,
false);
}[/code]
Here json is JSON object....
Am I doing right??
or I've to do something else to send JSON object since it is different
from string....
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---