Send JSON object via HttpPost method

by Alok Kulkarni » Tue, 08 Sep 2009 21:19:18 GMT

Sponsored Links
 Any answers ??


Send JSON object via HttpPost method

by Wayne Wenthin » Wed, 09 Sep 2009 05:39:03 GMT

 A snippet for what I do...
public HttpResponse updateGirl(String url, Pawn girl, int pawnID) {
HttpClient httpclient = new DefaultHttpClient();
HttpPut httpput = new HttpPut(url);

// Execute the request

try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
nameValuePairs.add(new BasicNameValuePair("id", Integer
......  Many nameValuePairs later.....

httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs,
 HttpResponse response = httpclient.execute(httpput);
return response;

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
return null;


Hope that helps.   I struggled with it for a couple of weeks before I found
a site that had a good source.

Writing code is one of few things
that teaches me I don't know everything.


Sponsored Links

Send JSON object via HttpPost method

by Alok Kulkarni » Wed, 09 Sep 2009 16:44:42 GMT

 ey Wayne, thanks a lot..
I have also got a solution parallely which i would like to post

URL url = new URL(serverURL);

// open the conncetion
HttpURLConnection connection =

// Let the run-time system (RTS) know that we want input.
// Let the RTS know that we want to do output
// No caching, we want the real thing
// set the content type property

// set request method
// create the post body to send
String content = credDevPair.toString();
Log.i("Request ====....... ",content);
DataOutputStream printout = new DataOutputStream (
connection.getOutputStream () );

// send the data
String output =
Log.i("Response 1....... ",output);
// A Simple JSONObject Creation
JSONObject json=new JSONObject(output);


// A Simple JSONObject Parsing
JSONArray nameArray=json.names();
JSONArray valArray=json.toJSONArray(nameArray);
for(int i=0; i<valArray.length() ;i++)


//BufferedReader input = new BufferedReader ( new
InputStreamReader(connection.getInputStream()) );

}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
}catch (IOException e) {
// TODO Auto-generated catch block

}catch(Exception ex)


private static String convertStreamToString(InputStream is) {
* To convert the InputStream to String we use the
* method. We iterate until the BufferedReader return null which
* there's no more data to read. Each line will appended to a
* and returned as String.
BufferedReader reader = new BufferedReader(new
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
} catch (IOException e) {
} finally {
try {
} catch (IOException e) {

return sb.toString();

On Wed, Sep 9, 2009 at 3:08 AM, Wayn

Send JSON object via HttpPost method

by Wayne Wenthin » Thu, 10 Sep 2009 08:58:06 GMT

 ep that is what I vaguely based mine on.   I recognize the Praeda line.   I
had to modify mine because I was not sending back an array of json objects
but a specific one that I handle based on the ID on the backend with ruby.

On Wed, Sep 9, 2009 at 1:43 AM, Alok Kulkarni <> wrote:

Writing code is one of few things
that teaches me I don't know everything.


Send JSON object via HttpPost method

by Alok Kulkarni » Thu, 10 Sep 2009 13:28:31 GMT

 k.. ya i had taken that code part from the forum itself. :)

On Thu, Sep 10, 2009 at 6:27 AM, Wayne Wenthin <> wrote:


Other Threads

1. Notification manager problem

Hi, I'm trying to set a notification from my application and when a
user click the notification it will open my application with the data.
the notification works fine, but the my application also get launch
when the notification being set. I thought the intent that I set up in
pending intent will only get launch when the notification is clicked
by the user.

The code for setting up the notification

 protected void showNotification() {
        // look up the notification manager service
        NotificationManager nm =

        Intent newIntent = new Intent(Intent.ACTION_VIEW, mUri, this,
        // The PendingIntent to launch our activity if the user
selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this,
                        newIntent, 0);

        // The ticker text, this uses a formatted string so our
message could be localized
        String tickerText =
getString(R.string.note_reminder_notification) + " " + mTitle;

        // construct the Notification object.
        Notification notif = new
Notification(R.drawable.ic_notification, tickerText,

        // Set the info for the views that show in the notification
        notif.setLatestEventInfo(this, mTitle, mNote, contentIntent);

        // after a 100ms delay, vibrate for 250ms, pause for 100 ms
        // then vibrate for 500ms.
        //notif.vibrate = new long[] { 100, 250, 100, 500};

        // Note that we use R.layout.incoming_message_panel as the ID
        // the notification.  It could be any integer you want, but we
        // the convention of using a resource id for a string related
        // the notification.  It will always be a unique number within
        // application.
        int notifId = R.string.note_reminder_notification + mId;
        nm.notify(notifId, notif);

Anybody can give me some pointer why it's doing this? Is there anyway
to get the sender info of an intent on the receiver side?


2. Context from which to initiate Service via Alarm Manager; Application Model Questions

I've created a simple app. to help me better understand a few key
components in the real app I'm making.  A snippet of what I've done:

- an Activity is launched (lets call it the Home Activity) -> from
here I schedule an alarm -> the alarm sets off a Pending Intent that
starts a Service.

- the above steps appear to do what I want them to do.

My questions:

- is a scheduled alarm persistent with respect to its creating process
(I believe so) ?  If the Home Activity is destroyed, the alarm still
goes off, correct?

- the PendingIntent that the alarm calls as well as the Intent nestled
within both have a Context of the Home Activity.  Is this a problem?
Does the system launch the Home Activity again (if it has been
destroyed since scheduling the alarm) just to launch the Service?

- the Service does its task (some networking which will take a couple
seconds) and then possibly creates a notification before killing
itself (the alarm is repeating so it will get called again later).
Does the Service need to make a new Thread to do its work in?  The
user is not interacting with the Home Activity at this point so I
don't think a new Thread is needed.

I think that sums up my questions for now.  Advice will be greatly


3. Windows ADB Driver?

4. adb devices command sees multiple offline devices attached - but it's the same device

5. SDCard and SQLiteOpenHelper problem

6. : How to stops the activity from getting restarted when mode changes from LandScape to Portrait or vice-versa

7. activity doesn't seem to end, despite calling .finish()...