1

I want to send few dictionaries from django to android through HTTP after getting query in HTTP get. How can I do this and what formatting should I use? I currently respond using HttpResponse. Name of keys are constant.

SuitUp
  • 3,112
  • 5
  • 28
  • 41

3 Answers3

1

Read about serializing objects in django.

You can choose between xml, json or yaml. It's pointless to add documentation here. Goto the link.

EDIT: Django's doc is really nice. Example shouldn't really be needed. But, still, an example from one of my projects [Line 492-507 from views.py].

def pendingOrders(request):
    userprof = UserProfile.objects.get(user= request.user)
    if userprof.is_student:
        student_account = request.user
        dish = Dishes.objects.all()
        #Getting all pending orders
        order_all_pending = Orders.objects.filter(student_id = student_account,delivered = False)
        pending_orders = Orders.objects.filter(~Q(status = 2),delivered = False)
        for order in order_all_pending:
            #Hack to change QuerySet to pass as JSON 
            order.quantity = pending_orders.filter(id__lt = order.id,counterid= order.counterid).count() + 1

        #Returning JSON response to the objects obtained in above statement
        return HttpResponse(serializers.serialize('json',order_all_pending,use_natural_keys=True),mimetype='application/json')
    else:
        return HttpResponse("Something went wrong")
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
  • Could you add some info about deserializing it in Java? – SuitUp Feb 05 '12 at 03:41
  • Can I ask you again about Java example? :) (please do not remove python snippet, I will use it too) – SuitUp Feb 05 '12 at 04:52
  • 1
    @SuitUp I gave you the python example because the serialization will happen at server side & you have tagged your post in django. If you are asking about parsing the JSON response returned from the server in java. see this [link](http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/). – shadyabhi Feb 05 '12 at 11:16
1

https://stackoverflow.com/a/2845612/931277 Has an example of parsing json from an HttpResponse in Android.

Community
  • 1
  • 1
dokkaebi
  • 9,004
  • 3
  • 42
  • 60
0

You should use JSON. Django even makes it easy for you.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358