我试图在远程服务器上运行一个应用程序,当我尝试注册一个用户它返回它。 相同的应用程序在我的笔记本电脑上建立服务器工作正常。我想知道我是否缺少对LINUX的依赖,以便在我的服务器端接收JSON。
org.json.JSONException:
的字符0处的输入结束
我的Android活动如下: -
private void registerUser(final String reg_FullName_value ,final String reg_Username_value, final String reg_Email_value, final String reg_Password_value)
{
// Tag used to cancel the request
String tag_string_req = "register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest sr = new StringRequest(Request.Method.POST, AppURLs.URL , new Response.Listener<String>(){
@Override
public void onResponse(String response)
{
Log.d("Response",response);
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error)
{
Toast.makeText(getApplicationContext(), "Regestered successfully", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Register.this, Login.class);
startActivity(intent);
finish();
setContentView(R.layout.login);
}
else
{
Toast.makeText(getApplicationContext(), "Not Regestered.", Toast.LENGTH_LONG).show();
}
}catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
"Some problem occur: check NetworK" , Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "register");
params.put("FullName", reg_FullName_value );
params.put("Username", reg_Username_value);
params.put("Email", reg_Email_value);
params.put("Password", reg_Password_value);
return params;
}
};
AppController.getInstance().addToRequestQueue(sr, tag_string_req);
}
处理此响应的PHP文件是: -
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
if(isset($_POST['tag']) && $_POST['tag'] != '')
{
$tag = $_POST['tag'];
require_once 'DB_Functions.php';
$db = new DB_Functions();
$response = array("tag" => $tag, "error" => FALSE);
//registration
if ($tag == 'register')
{
$name = $_POST['FullName'];
$username = $_POST['Username'];
$email = $_POST['Email'];
$password = $_POST['Password'];
$user = $db->storeUser($name, $username , $email , $password);
if ($user)
{
$response["error"] = FALSE;
$response["error_msg"] = "Registration Successful";
echo json_encode($response);
}
else
{
$response["error"] = TRUE;
$response["error_msg"] = "Registration Failed";
echo json_encode($response);
}
}
//login
else if ($tag == 'login')
{
$user = $_POST['Username'];
$password = $_POST['Password'];
$user_check = $db->getUserByUserAndPassword($user , $password);
if ($user_check != false)
{
$response["Success"] = TRUE;
$response["error"] = FALSE;
$response["error_msg"] = "User Exists";
$response["Username"] = $user;
echo json_encode($response);
}
else
{
$response["error"] = TRUE;
$response["error_msg"] = "No such User";
echo json_encode($response);
}
}
else
{
{
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect token or password!";
echo json_encode($response);
}
}
}
else
{
$response["error"] = TRUE;
$response["error_msg"] = "Recieved No Data";
echo json_encode($response);
}
?>
我正在使用Droplet制作云端服务器。我需要知道背后的原因是什么。
答案 0 :(得分:0)
试试这个。
$workspaceRoot
您应该先检查响应是否为空,而不是空字符串。
如果您的回复为空,则您的if(TextUtils.isEmpty(response)){
Toast.makeText(this, "response is null", Toast.LENGTH_SHORT).show();
return;
}
Log.d("Response",response);
将获得例外。