每个人,我最近都在开发服务器-客户端系统,在服务器端,我使用django和restframework与android应用程序进行交互。在客户端,我使用android和改造来从服务器发送和获取数据。当我从服务器获取数据时,一切都很好,但是当我尝试将数据从android发布到django时,它有一些错误。具体来说,它成功将数据发布到服务器,但未在django视图中显示。
我首先使用邮递员测试是否可以将数据发布到URL http://192.168.*.**:8001/api/output
,并且可以正常工作,但是当我使用android应用程序将数据发布到http://192.168.*.**:8001/api/output
时,它也可以成功发布>
这是我的django服务器代码
class OutputInfoView(ModelViewSet):
serializer_class = OutPutInfoSerializer
def get_queryset(self):
return models.OutputInfo.objects.all()
class OutPutInfoSerializer(serializers.ModelSerializer):
class Meta:
model = models.OutputInfo
fields = ['id', 'user', 'lx', 'ly', 'room', 'activity', 'sound']
# router url
routers = routers.DefaultRouter()
routers.register(r'output', views.OutputInfoView,basename='OutputInfo')
urlpatterns = [
path('', include(routers.urls))
# path('output', views.OutputInfoView.as_view(), name='output'),
]
在我的android客户端中,我进行了用户改造以发布数据:
public interface JsonPlaceHolderAPI {
@Headers({"Content-Type: application/json"})
@POST("output")
Call<List<Sender>> createUser(@Body RequestBody body);
}
这是主要活动负载
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.*.**:8001/api/") // restapi的BASE地址
.addConverterFactory(GsonConverterFactory.create()) // 添加jsonparse
.client(okHttpClient)
.build();
jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderAPI.class);
createSend();
// send post json data to server
private void createSend(){
Sender send = new Sender("2","35","43","sdf","gredf","4dfe");
Gson gson = new Gson();
String route = gson.toJson(send);
Log.d("TAG", route);
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
Call<List<Sender>> call = jsonPlaceHolderApi.createUser(body);
call.enqueue(new Callback<List<Sender>>() {
@Override
public void onResponse(Call<List<Sender>> call, Response<List<Sender>> response) {
if (response.isSuccessful()){
showText.setText("successfuly send");
}else{
showText.setText("Code: " + response.code());
return;
}
}
@Override
public void onFailure(Call<List<Sender>> call, Throwable t) {
showText.setText(t.getMessage());
}
});
}
在完成所有这些步骤之后,我首先使用postman来测试是否可以将数据发布到URL http://192.168.*.**:8001/api/output
,并且可以正常工作,但是当我使用android应用程序将发布数据发送到http://192.168.*.**:8001/api/output
时,它也可以成功发布,如下所示:
D/OkHttp: --> POST http://192.168.0.11:8001/api/output
D/OkHttp: Content-Type: application/json
Content-Length: 79
{"activity":"gredf","lx":"35","ly":"43","room":"sdf","sound":"4dfe","user":"2"}
--> END POST (79-byte body)
D/OkHttp: <-- 200 OK http://192.168.0.11:8001/api/output/ (94ms)
Date: Thu, 27 Jun 2019 02:30:55 GMT
Server: WSGIServer/0.2 CPython/3.7.1
Content-Type: application/json
Vary: Accept, Cookie
Allow: GET, POST, HEAD, OPTIONS
X-Frame-Options: SAMEORIGIN
Content-Length: 371
D/OkHttp: [{"id":1,"user":1,"lx":"25","ly":"35","room":"classroom","activity":"walk","sound":"speak"},{"id":2,"user":2,"lx":"12","ly":"79","room":"restroom","activity":"stand","sound":"silence"},{"id":3,"user":1,"lx":"22","ly":"19","room":"restroom","activity":"stand","sound":"silence"},{"id":4,"user":1,"lx":"22","ly":"19","room":"restroom","activity":"stand","sound":"silence"}]
<-- END HTTP (371-byte body)
恰当地,数据没有成功发送到服务器,我想知道哪里出了问题,我想我已经尽力了。
android将帖子数据发送到Django