doujionggan9570 2016-03-13 20:00
浏览 41

Volley Server错误(需要javascript?)Android开发

When trying to run my code to register a user via POST methods on my remote hosted database I get a mix of 2 errors in my app. The first being:

Volley Server Error

and second being an extremely long mess that essentially sums up to:

This site requires javascript to work. Please enable javascript in your browser.

Javascript Error

I'm largely confused because as far as I know I'm not using javascript in my code whatsoever. If anyone can help clear this up for me, I'd be eternally grateful. Here's the

register.php

<?php
require_once 'include/constants.php';
$con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];

$stmt = mysqli_prepare($con, "INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $email, md5($password));
$stmt->execute();

$stmt->close();

And my

Register.class

    package com.thinkgeist.stashbox;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by natha on 2016-03-10.
 */
public class Register extends AppCompatActivity implements View.OnClickListener {
    Button bRegister;
    EditText etUsername, etEmail, etPassword, etConfirm;
    PopupWindow popUp;
    LinearLayout mainLayout;

    public static final String KEY_USERNAME = "username";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASSWORD = "password";
    public static final String REGISTER_URL = "http://sb.thinkgeist.com/register.php";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        popUp = new PopupWindow(this);
        mainLayout = new LinearLayout(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etConfirm = (EditText) findViewById(R.id.etConfirm);
        etEmail = (EditText) findViewById(R.id.etEmail);
        bRegister = (Button) findViewById(R.id.bRegister);

        bRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.bRegister:
                // Responds to register button being clicked
                String username = etUsername.getText().toString();
                String email = etEmail.getText().toString();
                String password = etPassword.getText().toString();
                String passConfirm = etConfirm.getText().toString();

                if(password.length() == 0 || email.length() == 0 || username.length() == 0 || passConfirm.length() == 0){
                    Intent popUp = new Intent(this, Pop.class);
                    popUp.putExtra("error_message", "All fields must be filled in to register!");
                    startActivity(popUp);
                    break;
                }

                // Test if email is of proper format
                if(!email.contains("@")){
                    Intent popUp = new Intent(this, Pop.class);
                    popUp.putExtra("error_message", "Email is of wrong format!");
                    startActivity(popUp);
                    break;
                }

                // Test if password length is okay
                if(password.length() <= 5 || password.length() > 20){
                    Intent popUp = new Intent(this, Pop.class);
                    popUp.putExtra("error_message", "Password must be between 6-20 characters!");
                    startActivity(popUp);
                    break;
                }

                if(username.length() <= 5 || username.length() >= 20){
                    Intent popUp = new Intent(this, Pop.class);
                    popUp.putExtra("error_message", "Username must be between 6-20 characters!");
                    startActivity(popUp);
                    break;
                }
                if(!password.contentEquals(passConfirm)){
                    Intent popUp = new Intent(this, Pop.class);
                    popUp.putExtra("error_message", "Passwords do not match!");
                    startActivity(popUp);
                    break;
                }

                User registeredData = new User(username, email, password);
                Intent popUp = new Intent(this, Pop.class);
                registerNewUser(this, registeredData);
                popUp.putExtra("error_message", "Account sucessfully created!");
                //startActivity(popUp);
                break;

        }
    }

    public void registerNewUser(Context context, User toRegister){
        final String username = toRegister.username;
        final String password = toRegister.password;
        final String email = toRegister.email;

        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(Register.this,response,Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Register.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);
                params.put(KEY_EMAIL, email);
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
}

Sorry for the long post, but I feel it would be almost redundant to ask a question without providing enough information to have a chance at solving it. Thanks for anyone who takes their time out to help!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型