編輯:關於Android編程
pip install djangorestframework
django admin startproject restful
django admin startapp demo
修改restful/settings.py
在INSTALLED_APPS 添加 'rest_framework'
from django.db import models
# Create your models here.
class User(models.Model):
username = models.CharField(max_length=20,blank=True)
password = models.CharField(max_length=20,blank=True)
def __str__(self):
return self.username
python manage.py makemigratons
python manage.py migrate
python manage.py createsuperuser
from django.contrib import admin
# Register your models here.
from demo.models import User
admin.site.register(User)
此時登陸後台添加一些數據
(圖片不清楚可按住Ctrl+鼠標滾輪放大)
# author: HuYong
# coding=utf-8
from rest_framework import serializers
from demo.models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("id","username","password")
序列化聯通json與模型層
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/demo/',include('demo.urls')),
]
# author: HuYong
# coding=utf-8
from django.conf.urls import url
from demo import views
urlpatterns = [
url(r'^user/$',views.User_list),
url(r'^user/(?P[0-9]+)/$',views.User_detial),
]
import json
from django.shortcuts import render
# Create your views here.
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from demo.models import User
from demo.serializer import UserSerializer
@api_view(['GET','POST'])
def User_list(request):
if request.method=="GET":
users = User.objects.all()
serializer = UserSerializer(users,many=True)
return Response(serializer.data)
elif request.method == 'POST':
print request.body
serializer = UserSerializer(data=json.loads(request.body))
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET', 'PUT', 'DELETE']) #
def User_detial(request,pk):
try:
user = User.objects.get(pk=pk)
except User.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == "GET":
serializer = UserSerializer(user)
return Response(serializer.data)
elif request.method == "PUT":
serializer = UserSerializer(user,data=json.loads(request.body))
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
else:
return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
elif request.method == "DELETE":
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
最近做的項目是和語音實時采集並發送,對方實時接收並播放相關,下面記錄下實現的核心代碼。 很多Android開發者應該知道android有個MediaRecorder對象和
之前一直都是看別人寫的啟動模式,發現網上大多數的內容都是抄襲來抄襲去,直到最近看了開發藝術這本書,發現之前對啟動模式的理解過於簡單,很多東西都沒有考慮到,為了加深理解,於
Android日常開發中,對於下拉與上拉刷新控件的使用非常之頻繁。一般都會采用第三方庫,但是下拉刷新做到簡單優雅並不是太多,甚至有的兼容性都存在問題。這個是不能接受的。最
Android系統源碼閱讀(11):Android的InputManagerService的工作過程 請對照AOSP版本:6.0.1_r50。1. 創建InputMana