2015-07-12 42 views
-1

SOS。Django的HTTP请求中不包含任何东西

我真的很厌倦寻找我的简单问题。请帮帮我。

我有一个Android应用程序,使我的Django服务器的HTTP连接,并发送一个JSON和JSON接收请求。在我的代码在Android和Django。

但问题是我要求在Django不包含anything.no头和没有身体并没有什么else.and当我打印request.body我在原始数据的命令,看到的只是B“”。

请告诉我哪里错了。

tnx很多。

url.py

from django.conf.urls import include, url 
from django.contrib import admin 

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^post/', 'Post.views.post'), 
] 

view.py

from django.http import HttpResponse 


def post(request): 
    print(request.body) 
    return HttpResponse("This is my response", content_type='text/plain') 

setting.py

import os 

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 

SECRET_KEY = '-)lnh2s!-hiunefbnu9%[email protected](ztt*cfmq%8zj3-pg2+7l7' 

DEBUG = False 

ALLOWED_HOSTS = ['*'] 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'Post', 
) 

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.security.SecurityMiddleware', 
) 

ROOT_URLCONF = 'Test_Web_Service.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'Test_Web_Service.wsgi.application' 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 

STATIC_URL = '/static/' 

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'), 
) 

和我的Android代码:

PostClass.java

import android.content.Context; 

import com.google.gson.Gson; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 

import dao.User; 


public class PostClass { 

public static String Post(Context context) throws IOException { 
    Gson gson = new Gson(); 
    JSONObject object = new JSONObject();// 
    String jsonObjecetString; 

    User user = new User(); 
    user.setId(Long.parseLong("123")); 
    user.setName("someonesName"); 
    user.setPassword("secretPass"); 
    user.setPhone(Long.parseLong("123456789")); 

    jsonObjecetString = gson.toJson(user); 

    String content = ""; 
    BufferedReader reader = null; 

    try { 
     object.put("", jsonObjecetString); 


     String url = "http://192.168.1.12:8080/post"; 

     StringBuilder builder = new StringBuilder(); 

     HttpClient httpClient = new DefaultHttpClient(); 

     HttpPost request = new HttpPost(url); 

     StringEntity entity; 


     entity = new StringEntity(object.toString()); 

     request.setEntity(entity); 
     request.setHeader("Content-Type", "application/json"); 

     HttpResponse response = httpClient.execute(request); 
     int status = response.getStatusLine().getStatusCode(); 

     if (status == 200) // sucess 
     { 
      HttpEntity ex = response.getEntity(); 

      // String data = EntityUtils.toString(e); 
      InputStream content2 = ex.getContent(); 
      reader = new BufferedReader(new InputStreamReader(content2)); 

      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 

      content = builder.toString(); 
     } else if (status == 401) { 
      return "-Auth Failed Error Code 400"; 
     } else { 
      return "-Error Code: " + status; 
     } 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return content; 


} 
} 

回答

0

您的Java正在发送到URL'/ post',但Django正在侦听'/ post /'。附加斜杠中间件将从第一个重定向到第二个,但重定向始终是GET,因此主体将丢失。你应该首先发布到'/ post /'。