无法完成Http请求 - 错误500/403

时间:2013-07-30 07:37:08

标签: java android python django http-post

我试图从我的android应用程序发布HttpRequest到基于python的后端abd遇到500请求代码错误。有时,它给出了403

App POST请求

private String Register(String email,String password) {
        HttpClient httpclient = new DefaultHttpClient();
        String responseStr="";
        String URL=Constants.URL;
        System.out.println(URL);
        HttpPost httppost = new HttpPost(URL);

        try {
            // Add your data
            InetAddress ipAddress=null;
            try{
                ipAddress=InetAddress.getLocalHost();
                System.out.println("IP of my Android := "+ipAddress.getHostAddress());
            }catch (Exception e){
                System.out.println("Exception caught ="+e.getMessage());
            }
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("email", email));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            nameValuePairs.add(new BasicNameValuePair("ip", ipAddress.getHostAddress().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            System.out.println(email+","+password+","+ipAddress.getHostAddress().toString()+","+URL);
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            int responseCode = response.getStatusLine().getStatusCode();
            System.out.println("after this"+responseCode);
            switch(responseCode) {
            case 200:
                HttpEntity entity = response.getEntity();
                if(entity != null) {
                    String responseBody = EntityUtils.toString(entity);
                    responseStr=responseBody;
                }
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(Registration.this,"error", 
                    Toast.LENGTH_SHORT).show();
            //Constants.AlertBox("Failed", "Unable to register. Please try again", Registration.this);
        } 
        return responseStr;
    }

后端

@csrf_exempt       
def registration(request):
    registration_dict = {}
    #if 1==1 :
    if request.POST:
        email=request.POST['email']
        password=request.POST['password']
        ip=request.POST['ip']
        location=request.POST['location']
        #email="admin@admin.com"
        #password='123456'
        #ip='203.192.223.115'
        user = auth.authenticate(username=email, password=password)
        if user is None:
            user = User(username=email, password=password, email=email)
            user = UserInfo.objects.get_or_create(user=user, email_id=email)
            registration_dict["status"]="1"
            registration_dict["message"]="Thank You for registering"
            now=datetime.now()
            UserHistory.objects.create(user=user,location=location,authentication=1,date=now)
        else:
            registration_dict["status"]="2"
            registration_dict["message"]="User already registered"


        return HttpResponse(simplejson.dumps(registration_dict),content_type="application/json") 
    else:
        registration_dict["status"]="0"
        registration_dict["message"]="Unable to process the request"
        return HttpResponse(simplejson.dumps(registration_dict),content_type="application/json")

LogCat响应

07-30 18:17:10.459: I/System.out(9538):           <code>/Library/Python/2.6/site-packages/django/core/handlers/base.py</code> in <code>get_response</code>
07-30 18:17:10.459: I/System.out(9538):                 <ol start="104" class="pre-context" id="pre4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                    for middleware_method in self._view_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        response = middleware_method(request, callback, callback_args, callback_kwargs)</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        if response:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                            break</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre></pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                if response is None:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                    try:</pre></li></ol>
07-30 18:17:10.459: I/System.out(9538):               <ol start="111" class="context-line"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        response = callback(request, *callback_args, **callback_kwargs)</pre> <span>...</span></li></ol>
07-30 18:17:10.459: I/System.out(9538):                 <ol start='112' class="post-context" id="post4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                    except Exception, e:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        # If the view raised an exception, run it through exception</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        # middleware, and if the exception middleware returns a</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        # response, use that. Otherwise, reraise the exception.</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        for middleware_method in self._exception_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                            response = middleware_method(request, e)</pre></li></ol>
07-30 18:17:10.469: I/System.out(9538):                     <td>response</td>
07-30 18:17:10.529: I/System.out(9538): File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
07-30 18:17:10.529: I/System.out(9538):   111.                         response = callback(request, *callback_args, **callback_kwargs)
07-30 18:17:10.709: I/System.out(9538): this is login response 
07-30 18:17:10.749: I/System.out(9538):           <code>/Library/Python/2.6/site-packages/django/core/handlers/base.py</code> in <code>get_response</code>
07-30 18:17:10.749: I/System.out(9538):                 <ol start="104" class="pre-context" id="pre4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                    for middleware_method in self._view_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        response = middleware_method(request, callback, callback_args, callback_kwargs)</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        if response:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                            break</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre></pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                if response is None:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                    try:</pre></li></ol>
07-30 18:17:10.749: I/System.out(9538):               <ol start="111" class="context-line"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        response = callback(request, *callback_args, **callback_kwargs)</pre> <span>...</span></li></ol>
07-30 18:17:10.749: I/System.out(9538):                 <ol start='112' class="post-context" id="post4328052856"><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                    except Exception, e:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        # If the view raised an exception, run it through exception</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        # middleware, and if the exception middleware returns a</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        # response, use that. Otherwise, reraise the exception.</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                        for middleware_method in self._exception_middleware:</pre></li><li onclick="toggle('pre4328052856', 'post4328052856')"><pre>                            response = middleware_method(request, e)</pre></li></ol>
07-30 18:17:10.749: I/System.out(9538):                     <td>response</td>
07-30 18:17:10.759: I/System.out(9538): File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
07-30 18:17:10.759: I/System.out(9538):   111.                         response = callback(request, *callback_args, **callback_kwargs)

1 个答案:

答案 0 :(得分:-1)

您可以查看以下几项内容,

  1. 你无法在ui线程上调用http请求,将其移动到另一个线程或异步任务。

  2. 看起来你在本地机器上启动你的测试服务器,你需要确保它可以通过你的设备连接,你需要启动你的d​​jango服务器,python manage.py runserver 0.0.0.0:8000 < / p>