android - "POST /receiver/ HTTP/1.1" 500 137512 -
server side - python programming django framework:
views.py
from django.http import httpresponse django.shortcuts import render django.views.decorators.csrf import csrf_exempt import django.utils.simplejson json @csrf_exempt def rcvr(request): if request.method=='post': objs = request.post.get['username'] return render(request, "post.html",{'username': username}) else: return httpresponse("failure...")
urls.py
from django.conf.urls import patterns, include, url django.contrib import admin receiver import views admin.autodiscover() urlpatterns = patterns('', # examples: # url(r'^$', 'woodpecker.views.home', name='home'), # url(r'^woodpecker/', include('woodpecker.foo.urls')), # uncomment admin/doc line below enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # uncomment next line enable admin: url(r'^admin/', include(admin.site.urls)), url(r'^receiver/', views.rcvr, name='rcvr'), )
rcvr.html
<html> <head></head> <body> <form action="{% url 'woodpecker.receiver.views.rcvr' %}" method="post">{% csrf_token %} <input type="text" name="username"> <input type="submit" id="click1" value="username"> </form> </body> </html>
client side - android application
package com.example.postjson; import java.io.ioexception; import java.io.unsupportedencodingexception; import org.apache.http.httpresponse; import org.apache.http.client.*; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonobject; import android.app.activity; import android.os.bundle; import android.view.menu; public class mainactivity extends activity{ @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); httpclient httpclient = new defaulthttpclient(); try{ httppost post = new httppost("http://10.0.2.2:8000/receiver/"); post.setheader("content-type", "application/json"); post.setheader("accept","application/json"); jsonobject obj=new jsonobject(); obj.put("username", "abcd"); obj.put("password","1234"); post.setentity(new stringentity(obj.tostring(),"utf-8")); system.out.println("yo...yo...!! b-) "); httpresponse response = httpclient.execute(post); }catch (unsupportedencodingexception e) { e.printstacktrace(); }catch (clientprotocolexception e) { e.printstacktrace(); }catch (ioexception e) { e.printstacktrace(); }catch(nullpointerexception e){ e.printstacktrace(); }catch(exception e){ e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu){ getmenuinflater().inflate(r.menu.main, menu); return true; } }
and whenever run app simulator, 500 error...
[root@vivek-vaio woodpecker]# python manage.py runserver validating models... 0 errors found january 31, 2014 - 20:04:54 django version 1.5.4, using settings 'woodpecker.settings' development server running @ http://127.0.0.1:8000/ quit server control-c. [31/jan/2014 20:04:57] "get /receiver/ http/1.1" 200 10 [01/feb/2014 09:55:21] "post /receiver/ http/1.1" 500 137512 internal server error: /receiver/ traceback (most recent call last): file "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response response = callback(request, *callback_args, **callback_kwargs) file "/usr/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view return view_func(*args, **kwargs) file "/home/vivek/project/server_work/woodpecker/receiver/views.py", line 21, in rcvr return render(request, "rcvr.html",{'username': objs}) file "/usr/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 53, in render return httpresponse(loader.render_to_string(*args, **kwargs), file "/usr/lib/python2.7/site-packages/django/template/loader.py", line 177, in render_to_string return t.render(context_instance) file "/usr/lib/python2.7/site-packages/django/template/base.py", line 140, in render return self._render(context) file "/usr/lib/python2.7/site-packages/django/template/base.py", line 134, in _render return self.nodelist.render(context) file "/usr/lib/python2.7/site-packages/django/template/base.py", line 830, in render bit = self.render_node(node, context) file "/usr/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node return node.render(context) file "/usr/lib/python2.7/site-packages/django/template/defaulttags.py", line 404, in render raise noreversematch("'url' requires non-empty first argument. " noreversematch: 'url' requires non-empty first argument. syntax changed in django 1.5, see docs. [01/feb/2014 09:55:33] "post /receiver/ http/1.1" 500 137512
can please me?? 500 error???
you're missing steps in view enable csrf. i'm assuming csrf problem, throws 403s if not enabled correctly.
Comments
Post a Comment