无法在我的Android应用程序与服务器之间建立连接

时间:2019-10-27 15:25:58

标签: java android

这是网址'http://localhost:8080/shopping/' 我只知道tomcat和MySQL已连接,并且服务器端没有错误

我认为该错误是由于android应用和tomcat服务器之间的连接代码

错误: this is the screenshot of errors

文本错误:

System.err:位于spoorthy.shopping.RegisterThread.run(RegisterThread.java:31)

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'Boolean java.lang.String.equals(java.lang.Object)'

System.err:在spoorthy.shopping.Register.register(Register.java:93)

System.err:在spoorthy.shopping.Register $ 1.onClick(Register.java:28)

这是Android中ServerURL.java文件的内容

    package spoorthy.shopping;
    public class ServerURL {
    static String url;
    public static void setURL(String u){
        url = "http://"+u+":8080/shopping/";
    }
    public static String getURL(){
        return url;
    }
}

这是MainActivity.java文件的内容

package spoorthy.shopping;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
    EditText ip;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initClickListner();
    }
    private void initClickListner() {
        ip = (EditText)findViewById(R.id.ip);
        Button admin = (Button) findViewById(R.id.login);
        admin.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                String s1 = ip.getText().toString().trim();
                ServerURL.setURL(s1);
                Intent intent = new Intent(MainActivity.this, Login.class);
                startActivity(intent);
            }
        });
        Button customer = (Button) findViewById(R.id.register);
        customer.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                String s1 = ip.getText().toString().trim();
                ServerURL.setURL(s1);
                Intent intent = new Intent(MainActivity.this, Register.class);
                startActivity(intent);
            }
        });



    }

}

这是register.java文件的内容

   package spoorthy.shopping;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Register extends Activity {
    EditText user,pass,place,email,contact;
    Button signup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);
        place = (EditText) findViewById(R.id.address);
        email = (EditText) findViewById(R.id.mail);
        contact = (EditText) findViewById(R.id.contact);

        signup=(Button) findViewById(R.id.signup);
        signup.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                try{
                    register();
                }catch(Exception e){
                    Toast.makeText(Register.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
    }
    public void register(){
        //read values from fields
        String s1 = user.getText().toString();
        String s2 = pass.getText().toString();
        String s3 = place.getText().toString();
        String s4 = email.getText().toString();
        String s5 = contact.getText().toString();

        //sb.append("1of"+list.size());
        //check values should not be empty
        if(s1.trim().length() == 0 ||s1 == null){
            Toast.makeText(Register.this, "Please enter username", Toast.LENGTH_LONG).show();
            user.requestFocus();
            return;
        }
        if(s2.trim().length() == 0 ||s2 == null){
            Toast.makeText(Register.this, "Please enter password", Toast.LENGTH_LONG).show();
            pass.requestFocus();
            return;
        }
        if(s3.trim().length() == 0 ||s3 == null){
            Toast.makeText(Register.this, "Please enter address", Toast.LENGTH_LONG).show();
            place.requestFocus();
            return;
        }
        if(s4.trim().length() == 0 ||s4 == null){
            Toast.makeText(Register.this, "Please enter emailid", Toast.LENGTH_LONG).show();
            email.requestFocus();
            return;
        }
        if(!CheckMail.checkMail(s4)){
            Toast.makeText(Register.this, "Please enter valid email id", Toast.LENGTH_LONG).show();
            email.requestFocus();
            return;
        }
        if(s5.trim().length() == 0 || s5 == null){
            Toast.makeText(Register.this, "Please enter contact no", Toast.LENGTH_LONG).show();
            contact.requestFocus();
            return;
        }
        StringBuilder request = new StringBuilder();
        request.append("t1="+s1+"&t2="+s2+"&t3="+s3+"&t4="+s4+"&t5="+s5);
        //progress dialog
        ProgressDialog dialog = new ProgressDialog(Register.this);
        dialog.setMessage("Processing...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
        //call thread
        RegisterThread register = new RegisterThread(request.toString());
        try{
            //wait till thread complete its execution
            register.join();
            dialog.dismiss();
            //get response from server
            String res = register.getResponse();
            //if response successfull then go to LoginActivity
            if (res.equals("success")) {
                Intent in1 = new Intent(Register.this,Login.class);
                startActivity(in1);
            } else {
                //show login fail
                Toast.makeText(Register.this, "failed to register", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(Register.this, e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
    public void printError(String msg){
        user.setText(msg);
        Toast.makeText(Register.this, msg, Toast.LENGTH_LONG).show();
    }
}

这是我的RegisterThread.java文件的内容

package spoorthy.shopping;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class RegisterThread extends Thread{
    //query variable
    String request;
    //response from server
    String response;

    //return server response
    public String getResponse(){
        return response;
    }
    //assign query here. This query contains all registration values
    public RegisterThread(String request){
        this.request = request;
        //call to run
        start();
    }
    //start network connection
    public void run(){
        try{
            URL url = new URL(ServerURL.getURL()+"Register");
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
            bw.write(request);
            bw.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            response = br.readLine();
        }catch(Exception e){
            //register.printError(e.getMessage());
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案