How to send JSONObject by HttpPost?

by veen » Sat, 26 Sep 2009 02:17:26 GMT


Sponsored Links
 I need to send http request with the following view:

POST /device/api/login HTTP/1.1
Content-Type: text/json

{"login":"LOGIN","password":"PASSWORD"}

[my code]
                        DefaultHttpClient hClient = new
DefaultHttpClient();
                        HttpPost post = new HttpPost(host + 
"/device/api/login");

                        post.setHeader("Content-Type", "text/json");

                        JSONObject jo = new JSONObject();
                        jo.put("login", login);
                        jo.put("password", password);
                        String  joSt = jo.toString();

                        BasicHttpEntity be = new BasicHttpEntity();
                        byte bytes[] = joSt.getBytes();

                        be.setContent(new ByteArrayInputStream(bytes));
                        post.setEntity(be);

                        HttpResponse response = hClient.execute(post);

                        hClient.getConnectionManager().shutdown();
[/my code]
It doesn't work. Could you help me to figure out what's wrong?

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



How to send JSONObject by HttpPost?

by Alok Kulkarni » Sat, 26 Sep 2009 03:25:03 GMT


 njoy....
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();
}

Please put in the requred content type, server url..
Thanks, Alok

On Fri, Sep 25, 2009 at 2:01 AM, veen <veensmail...@gmail.


Sponsored Links


Other Threads

1. Help.... trouble granting DEVICE_POWER permission

I'm trying to develop an application that need DEVICE_POWER
permission, I put it in android.manifets file, but when launching the
app crashes, the logcat reports "Not granting permission
DEVICE_POWER..." due to Security constraints.

Checking the docs it seems that the apk must be signed with a real
certificate, anyone that knows more about it?

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

2. Screen color depth supported by Android 1.5/Galaxy

Hi,

there's a discussion going on about the color depth on the Samsung
Galaxy.

While it is advertised as having a 16 Million color display (24bpp) it
is only running at 16bpp:

    $ cat /sys/devices/virtual/graphics/fb0/bits_per_pixel
    16

Some say that it is a restriction of Android 1.5 itself which was only
capable of supporting up to 16bpp screens. Android would be down
sampling to 16bpp whatever the screen supports.

I've searched the available documentation over and over and can't find
anything like that.

As I see it Android 1.5 would indeed be capable of displaying 24bpp
images properly given that the screen is capable of and configured to
do so.

Can anyone give an authoritative answer, please?

Kind regards, Felix

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

3. How to get the duration of an audio resource using SoundPool

4. What Happened to Android Discuss Group?

5. getting Java Exception, libmedia_jni.so already loaded

6. Handset screen sizes

7. How to manage service lifecycle when device kills it due to low memory