Enjoy....
  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 <[email protected]> wrote:

>
> 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?
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to