Friday, July 30, 2010

[android-developers] Re: Help Needed Getting JSON to Jersey Web Service and Consuming it

Thanks very much Mr Gies, I'll digest this later and expand upon my
requirements if still required.

Cheers.

On 30 July, 17:25, Brad Gies <rbg...@gmail.com> wrote:
> To give you good advice, we probably need to know what your server end
> is expecting, and what kind of security you need.
>
> It could be as simple as what's below (NOTE there is extra stuff you
> don't need in here, but it should at least give you what you need) :
>
> List<NameValuePair> nvps;
>
>          try
>          {
>              InputStream is = null;
>
>              DefaultHttpClient httpclient = new DefaultHttpClient();
>              HttpPost httpPost = new HttpPost(urlString);
>
>              String searchParams = getSearchParams();  // just my
> function to get what I want to pass
>
>               searchParams = URLEncoder.encode(searchParams, HTTP.UTF_8);
>
>               nvps.add(new BasicNameValuePair("category", searchParams));
>
>              httpPost.setHeader("User-Agent", "iHotTonight Android
> App");  // I set this so my server logs files are easier to read.
>              httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
>              HttpResponse response = httpclient.execute(httpPost);
>
>              HttpEntity entity = response.getEntity();
>
>              if (entity != null)
>              {
>                  try
>                  {
>                      is = entity.getContent();
>
>                      String line;
>                      StringBuilder builder = new StringBuilder();
>                      BufferedReader reader = new BufferedReader(new
> InputStreamReader(is), 8192);
>                      while ((line = reader.readLine()) != null)
>                      {
>                          builder.append(line);
>                      }
>                      String retStr = builder.toString();
>                      // Now get any messages coming back.
>                      if (BistroSharedStatic.DEBUGGING_APP)  // I set
> this if I want to log my results.
>                          Log.d("GetResults", retStr);
>
>                      if (handler != null)   // if I passed a Handler to
> the create then send the result to the Handler.
>                          sharedFunctions.SendMessage(handler,
> HandlerReturnMessage, param1, param2, retStr);
>
>                  }
>                  finally
>                  {
>                      entity.consumeContent();
>                      is = null;
>                  }
>              }
>
>          }
>          catch (MalformedURLException e)
>          {
>              // TODO Auto-generated catch block
>              e.printStackTrace();
>              if (handler != null)
>                  sharedFunctions.SendMessage(handler, 0, 0, 0,
> e.getMessage());  // if I passed in a Handler then send the exception to
> the handler.
>          }
>          catch (IOException e)
>          {
>              // TODO Auto-generated catch block
>              e.printStackTrace();
>              if (handler != null)
>                  sharedFunctions.SendMessage(handler, 0, 0, 0,
> e.getMessage());
>          }
>          catch (Exception e)
>          {
>              e.printStackTrace();
>              if (handler != null)
>                  sharedFunctions.SendMessage(handler, 0, 0, 0,
> e.getMessage());
>          }
>
> AND in PHP on the server end:
>
> $username=$_POST['from'];
> $password=$_POST['theirpassword];"
> $json_string=$_POST['searchwords'];
>
> // do the usual stripslashes and mysql_real_escape_string stuff here.
>
> // now, process the JSON string
> $json = json_decode($json_string);
>
> BUT... depending on the security etc... it can be a lot more
> complicated. Hopefully, this is enough to get you started. :)
>
> On 30/07/2010 7:33 AM, JimBadger wrote:
>
>
>
>
>
> > Hi,
>
> > I've just recently started out with Android development, and have
> > progressed to the point where I have written a very basic Hello World
> > Jersey RESTful web service, and successfully called it from within my
> > Android App.
>
> > What I want to do now though, is send a new user record (just name and
> > password for now) to a web service in JSON format.
>
> > I can create my JSON in the Android app just fine, but I don't know
> > how to get it into the HTTP Request in such a way that the
> > corresponding web service that @Consumes JSON can get at it.
>
> > So, this is how far I've got:
>
> > 1) Create Android App User object...(Tick: done that)
> > 2) Turn that into JSON...(Tick: done that)
> > 3) Put it on the HTTP request.....(Stuck here! Help!)
> > 4) Call the web service...(Tick: done that)
> > 5) Web service retrieve JSON from request...(Stuck here! Help!)
> > 6) Parse the JSON into server side User object and deal with as I see
> > fit...(Don't worry people, I'll be fine with this bit, thanks!)
> > 7) Profit! ;)
>
> > Guys and Girls, I really appreciate any help given, you dear, dear
> > kind souls!
>
> > Cheers,
> > James
>
> --
> Sincerely,
>
> Brad Gies
> -----------------------------------------------------------------------
> Bistro Bot - Bistro Blurbhttp://www.bgies.comhttp://www.bistroblurb.comhttp://www.ihottonight.com
> -----------------------------------------------------------------------
>
> Never doubt that a small group of thoughtful, committed people can
> change the world. Indeed. It is the only thing that ever has - Margaret Mead- Hide quoted text -
>
> - Show quoted text -

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

No comments:

Post a Comment