我花了很多时间来搜索如何解决我的问题但是不成功,所以我跳了一些可以帮助我。
我用android studio制作我的应用程序: 我用登录和httm请求完成我的页面(100%工作以构思并创建新帐户)现在我尝试向我的服务器发出Get请求并获取用户名rerver转换我尝试在字符串上强制转换的字节数组并获取只是null“”。
有我的代码: mainActivity.java
Button b = (Button)findViewById(R.id.b2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SyncHttpClient client = new SyncHttpClient();
RequestParams params = new RequestParams();
params.put("email", "test3@t.com");
// Handle request
client.get("https://......../user/get/", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
System.out.println("response ----" + response);
String res = new String(response); //convetion Byte[] to String that i use
Context context = getApplicationContext();
Toast.makeText(context, res, Toast.LENGTH_SHORT).show();
System.out.println("res ----" + res);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
System.out.println("errorResponse -----"+errorResponse);
String res = new String(errorResponse);
Context context = getApplicationContext();
// Toast.makeText(context, res, ast.LENGTH_SHORT).show();
System.out.println("ERROR--" + res);
}
});
}});
服务器端看起来像这样: views.py
def get_users(req):
"""
Use url :
https://...../user/get/
With arguments :
?email=EMAIL
"""
email = req.GET.get('email')
if email is not None:
exists = User.objects.filter(email=email)
if len(exists) == 0:
return HttpResponse("Error invalid email !")
else:
res = "%s:%s" %(exists[0].username, exists[0].points)
return HttpResponse(exists[0].username)
else:
return HttpResponse("Missing information !")
我在Android Monitor上获得的内容:
I/System.out: response------[B@4136b800
I/System.out: res --------
你可以在转换后看到我只有NULL。
有人可以帮我PLZ