问题描述
- Android实现简单的账号注册功能时JSON报错?
-
最近在做一个对接融云的聊天界面,在实现注册功能时报错。具体如下:
通过Android客户端注册账号,服务器和数据库用Apache+php+Mysql+phpmyadmin。
Android主要代码
final String username = et_username.getText().toString();
String password = et_password.getText().toString();if (username.equals("") || password.equals("")) { Toast.makeText(RegActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show(); } else { AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.add("username", username); params.add("password", password); client.post("http://192.168.1.101/chat/reg.php", params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { String response = new String(bytes); Log.e("debug", response); JSONObject object = null; try { object = new JSONObject(response); String status = object.getString("status"); if (status.equals("exists")) { Toast.makeText(RegActivity.this, "用户名已存在,请更换", Toast.LENGTH_LONG).show(); } else if (status.equals("error")) { Toast.makeText(RegActivity.this, "出现错误,请稍后重试", Toast.LENGTH_LONG).show(); } else if (status.equals("success")) { String token = object.getString("token"); App.token = token; App.username = username; App.isLogin = true; Intent intent = new Intent(RegActivity.this, MainActivity.class); startActivity(intent); RegActivity.this.finish(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { Toast.makeText(RegActivity.this, "网络错误,请稍后重试", Toast.LENGTH_LONG).show(); } });
** D:wampApache22htdocschat中reg.php代码**
<?php
require_once('./conn.php');
require_once('./api.php');if(isset($_POST['username']) && isset($_POST["password"]))
{
$password = md5($_POST['password']);$sql = "select * from `user` where username='$_POST[username]'"; $query = mysqli_query($connect, $sql); $row = mysqli_num_rows($query); if($row == 0) { $p = new ServerAPI("vnroth0krcc8o", "77S067I5vYC"); $r = $p->getToken($_POST['username'], "", ""); $obj = json_decode($r); if($obj->code != 200) { $result = array("status" => "error"); echo json_encode($result); } else { $token = $obj->token; $sql2 = "insert into `user` (username, password, token) values('$_POST[username]', '$password', '$token')"; $query = mysqli_query($connect, $sql2); $result = array("status"=>"success", "token"=>$token); echo json_encode($result); } } else { $result = array("status"=>"exists"); echo json_encode($result); }
}
?>
点击注册填写账号密码后报错信息是
04-30 02:25:16.236 6715-6801/com.gzs.jike_chat D/OpenGLRenderer: endAllStagingAnimators on 0xa46aad00 (RippleDrawable) with handle 0xa44ae3a0
04-30 02:25:27.004 6715-6728/com.gzs.jike_chat I/art: Background sticky concurrent mark sweep GC freed 1590(58KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 2MB/2MB, paused 12.793ms total 27.808ms
04-30 02:25:36.852 6715-6715/com.gzs.jike_chat V/AsyncHttpResponseHandler: Progress 174 from 174 (100%)
04-30 02:25:36.852 6715-6715/com.gzs.jike_chat E/debug: 用户名称 不能为空Notice: Trying to get property of non-object in D:wampApache22htdocschatreg.php on line 16
{"status":"error"}
04-30 02:25:36.852 6715-6715/com.gzs.jike_chat W/System.err: org.json.JSONException: Value 用户名称 of type java.lang.String cannot be converted to JSONObject
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at org.json.JSONObject.(JSONObject.java:160)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at org.json.JSONObject.(JSONObject.java:173)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at com.gzs.jike_chat.RegActivity$1$1.onSuccess(RegActivity.java:54)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:311)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:138)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at android.os.Looper.loop(Looper.java:135)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-30 02:25:36.853 6715-6715/com.gzs.jike_chat W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-30 02:25:48.368 6715-6723/com.gzs.jike_chat W/art: Suspending all threads took: 6.184ms
在网上没找到解决方案,有的说是jar包版本问题,我在lib只导入一个android-async-http-1.4.6.jar,请大神指导下,该如何解决这个问题?谢谢!
解决方案
Log.e("debug", response);
这个输出是什么,可能是乱码吧?
04-30 02:25:36.852 6715-6715/com.gzs.jike_chat W/System.err: org.json.JSONException: Value 用户名称 of type java.lang.String cannot be converted to JSONObject
1.首先这个返回值可能不是json 格式,这里需要try catch 一下;失败就跳出去,从log查原因;
解决方案二:
你应该好好看看融云的API文档