[android-beginners] Re: Send JSON object via HttpPost method

2009-09-09 Thread Alok Kulkarni
Hey 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; ivalArray.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, Wayne Wenthin wa...@fuligin.com wrote:

 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 {
 ListNameValuePair nameValuePairs = new ArrayListNameValuePair(
  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.



 On Tue, Sep 8, 2009 at 6:18 AM, Alok Kulkarni kulsu...@gmail.com wrote:

 Any answers ??


 On Tue, Sep 8, 2009 at 2:00 PM, Alok kulsu...@gmail.com wrote:


 I have created a JSON object which i want to send over the network to
 a server.Do i need to user OutputStream and BufferedOutput stream ?
 I have read the RestClient example which parses an incoming JSON
 object and retrieves the string data. But how do i send a JSON object
 to the server?
 Thanks,
 Alok







 --
 Writing code is one of few things
 that teaches me I don't know everything.

 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Beginners group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to

[android-beginners] Re: Send JSON object via HttpPost method

2009-09-09 Thread Wayne Wenthin
Yep 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:

 Hey 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; ivalArray.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, Wayne Wenthin wa...@fuligin.com wrote:

 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 {
 ListNameValuePair nameValuePairs = new ArrayListNameValuePair(
  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.



 On Tue, Sep 8, 2009 at 6:18 AM, Alok Kulkarni kulsu...@gmail.com wrote:

 Any answers ??


 On Tue, Sep 8, 2009 at 2:00 PM, Alok kulsu...@gmail.com wrote:


 I have created a JSON object which i want to send over the network to
 a server.Do i need to user OutputStream and BufferedOutput stream ?
 I have read the RestClient example which parses an incoming JSON
 object and retrieves the string data. But how do i send a JSON object
 to the server?
 Thanks,
 Alok







 --
 

[android-beginners] Re: Send JSON object via HttpPost method

2009-09-08 Thread Alok Kulkarni
Any answers ??

On Tue, Sep 8, 2009 at 2:00 PM, Alok kulsu...@gmail.com wrote:


 I have created a JSON object which i want to send over the network to
 a server.Do i need to user OutputStream and BufferedOutput stream ?
 I have read the RestClient example which parses an incoming JSON
 object and retrieves the string data. But how do i send a JSON object
 to the server?
 Thanks,
 Alok
 


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



[android-beginners] Re: Send JSON object via HttpPost method

2009-09-08 Thread Wayne Wenthin
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 {
ListNameValuePair nameValuePairs = new ArrayListNameValuePair(
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.



On Tue, Sep 8, 2009 at 6:18 AM, Alok Kulkarni kulsu...@gmail.com wrote:

 Any answers ??


 On Tue, Sep 8, 2009 at 2:00 PM, Alok kulsu...@gmail.com wrote:


 I have created a JSON object which i want to send over the network to
 a server.Do i need to user OutputStream and BufferedOutput stream ?
 I have read the RestClient example which parses an incoming JSON
 object and retrieves the string data. But how do i send a JSON object
 to the server?
 Thanks,
 Alok



 



-- 
Writing code is one of few things
that teaches me I don't know everything.

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