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. setItemChecked versus Cursor

I have a multiple choice listview where I'm trying to implement
"Select All" and "Select None" buttons.  What I'm doing is moving the
cursor to the first row, then going through the list one by one and
setting the item as checked.  As soon as I set the item as checked
(true or false), the list cursor gets reset back to the first item in
the list.

This code becomes an infinite loop because it keeps resetting back to
item 0 every time:

mListCursor.moveToFirst();
ListView list = getListView();
do
{
    list.setItemChecked(mListCursor.getPosition(), true);
} while(mListCursor.moveToNext());

I've added some code to save the position before calling
setItemChecked and then moving the cursor to that position afterward
and that fixes the problem.

Can anyone tell me if I'm doing something wrong?  Should I be doing
this a different way?

Thanks

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

2. What could we do with Android with this kind of investment...

Google doesn't have to worry. There's always the federal bailouts just
around the corner for failed companies!

take care,
Muthu Ramadoss.

http://linkedin.com/in/tellibitz +91-9840348914
http://androidrocks.in - Android Consulting.







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

3. FAILED Binder Transaction !

4. JNI run time error even I use same code and lib as Android source code.

5. How to customize Gallery Widget in android?

6. How to prevent my application being closed on clicking the back button on emulator

7. Learn Android Programming Online