gpt4 book ai didi

java - 无法完成 HttpRequest - 错误 500/403

转载 作者:行者123 更新时间:2023-12-01 06:18:21 24 4
gpt4 key购买 nike

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

应用 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. 您不能在 ui 线程上调用 http 请求,也不能将其移至另一个线程或异步任务。

  2. 看起来你在本地计算机上启动了测试服务器,你需要确保它可以通过你的设备连接,你需要启动你的 django 服务器, python manage.py runserver 0.0.0.0:8000

关于java - 无法完成 HttpRequest - 错误 500/403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17940965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com