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>(
39);
nameValuePairs.add(new BasicNameValuePair("id", Integer
.toString(pawnID)));
......  Many nameValuePairs later.....


httpput.addHeader("Content-Type",
"application/x-www-form-urlencoded");
httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
 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
e.printStackTrace();
}
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 =
(HttpURLConnection)url.openConnection();

// Let the run-time system (RTS) know that we want input.
connection.setDoInput(true);
// Let the RTS know that we want to do output
connection.setDoOutput(true);
// No caching, we want the real thing
connection.setUseCaches(false);
// set the content type property
connection.setRequestProperty("Content-type",strContenttype);

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

// send the data
printout.writeBytes(content);
printout.flush();
printout.close();
String output =
convertStreamToString(connection.getInputStream());
Log.i("Response 1....... ",output);
// A Simple JSONObject Creation
JSONObject json=new JSONObject(output);

Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");

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

Log.i("Praeda","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"

+"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
}
//BufferedReader input = new BufferedReader ( new
InputStreamReader(connection.getInputStream()) );

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

}catch(Exception ex)
{

}

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

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

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 <kulsu...@gmail.com> 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 <wa...@fuligin.com> wrote:


--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. My BroadcastReceiver is not receiving the BOOT_COMPLETED intent after my N1 boots. Help Please!!!

I am unable to get my BroadcastReceiver onReceive method called using
the BOOT_COMPLETED intent.

-- AndroidManifest.xml --
<application android:icon="@drawable/icon" android:label="@string/
app_name"
                android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".activities.Umbrella"
android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.Preferences"
android:label="@string/app_name" android:screenOrientation="portrait" /
        <receiver
android:name="com.jerrellmardis.umbrella.receiver.WeatherStartupReceiver">
                <intent-filter>
                        <action 
android:name="android.intent.action.BOOT_COMPLETED" /
                </intent-filter>
        </receiver>
        <service android:name=".service.WeatherUpdateService">
                <intent-filter>
                        <action
android:name="com.jerrellmardis.umbrella.service.WeatherUpdateService" /
                </intent-filter>
        </service>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" /
        <uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

-- WeatherStartupReceiver.java --
package com.jerrellmardis.umbrella.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Contacts.People;
import android.util.Log;

import com.jerrellmardis.umbrella.R;

public class WeatherStartupReceiver extends BroadcastReceiver {

        private NotificationManager mNotificationManager;
        private int SIMPLE_NOTFICATION_ID;

        @Override
        public void onReceive(Context context, Intent intent) {
                 // Do something interesting here...
        }
}

-- 

2. where comes --custom-target while building market_licensing

Hello,

I would like to add the option "--custom-package" for the generate
resources step of my application as it happens while the
market_licensing library is build:

     [null] ...
     [null] Executing '/usr/local/android-sdk-linux/platforms/
android-8/tools/aapt' with arguments:
     [null] 'package'
     [null] '-f'
     [null] '-m'
     [null] '--custom-package'
     [null] 'com.android.vending.licensing'
     [null] ...

how can it be done with less pain?
(like adding custom.package=my.pkg.name in build.properties)


And btw. is there a human friendly alternative to Ant / xml-hell,
which can be used together with android?

regards,
Alexej

-- 

3. Get the GPS when I access a website by Android phone

4. How to discover and connect a particular type of BluetoothDevice

5. Integrating Barcode Scanner

6. Linux Kernel Motion Events

7. Regarding survey of android..