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. N1 Resmi

Halo RR,
Apakah mungkin N1 (google) menjual N1 via Dirjen Postel ? Bukan BM spt yg skg ?

Powered by Telkomsel BlackBerry

-- 
"Indonesian Android Community [id-android]" 

2. DB Android Blogger & Android Developer

Hi,

What's the Difference Between Android Blogger & Android Developer?...


-- 
Thanks & Regards
Sasikumar.S

-- 

3. Keyboard Typing Error

4. GridView for Buttons, not images

5. Another flight information apk

6. eclipse help cant find canvas?

7. TextView with Clear Button