从Android

时间:2017-12-12 05:31:44

标签: android server client remote-server

我正在开发一个简单的消息传递应用。该应用程序的第一页是登录屏幕。它使用Credentials和Client类来允许访问。代码在进入Credentials类之前失败了。这是代码

MainActivity.java

package com.mchat.messagingapp;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import com.mchat.messagingapp.Credentials;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import com.mchat.messagingapp.Credentials;
import static java.security.AccessController.getContext;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

}
int attempts = 3;

// The login button has been clicked
public void onButtonClick(View view) {

    EditText PW_ = findViewById(R.id.editText2);
    EditText ID_ = findViewById(R.id.ID);

    //Some hashing stuff was here

    //Run server request to get user data
    try {
        Credentials c = new Credentials();
        Client client = new Client("myipaddress",PORTNUM); //random values for question
        ServerMessage message = client.readServerMessage();

        client.sendMessage(ClientMessageType.LOGIN, ID_.getText().toString());
        message = client.readServerMessage();
        System.out.println(message.getMessage());
        c = ReadMessage.returnCred(message.getMessage());
        System.out.println(c.getUsername());


    // loging in
    if (c.getLocked() == "1") {
        if (hassedPass.equals(c.getPsword())) {
            if (c.getAdmin() == "1") {
                //go to admin page
                Intent intent2 = new Intent(MainActivity.this, AdminList.class);
                startActivity(intent2);
            } else {
                //go to normal page
                Intent intent = new Intent(MainActivity.this, Mainpage.class);
                startActivity(intent);
            }
        } else {
            attempts--;
            Toast.makeText(this.getApplicationContext(), "Incorrect Login, " + attempts + " attempts remaining", Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(this.getApplicationContext(), c.getLocked(), Toast.LENGTH_SHORT).show();
        Toast.makeText(this.getApplicationContext(), "ACCOUNT LOCKED! Please go see Tech Support", Toast.LENGTH_SHORT).show();
    }
    } catch (ClientException e) {
        System.out.println(e);
    }
}

public void on_login(View view) {

    if(attempts > 0) {
        onButtonClick(view);
    }
    else{
        Toast.makeText(this.getApplicationContext(), "Too many failed attempts",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
}

}

Client.java

package com.mchat.messagingapp;

import java.io.*;
import java.net.Socket;

public class Client
{
private Socket serverSock;
private BufferedReader inStream;
private BufferedWriter outStream;

public Client(String server, int port) throws ClientException
{
    try
    {          
        this.serverSock = new Socket(server, port);

        this.inStream = new BufferedReader(new InputStreamReader(this.serverSock.getInputStream()));
        this.outStream = new BufferedWriter(new OutputStreamWriter(this.serverSock.getOutputStream()));
    }
    catch(Exception e)
    {
        throw new ClientException("Encountered an error while initializeing client!", e);
    }
}

public void close() throws ClientException
{
    try
    {
        this.sendMessage(ClientMessageType.HANGUP);
        this.serverSock.close();
    }
    catch (Exception e)
    {
        throw new ClientException("Encountered an error while closing client connection!", e);
    }
}

public ServerMessage readServerMessage() throws ClientException
{
    try
    {
        String message = this.inStream.readLine();
        System.out.println("Server sent: " + message);

        this.sendMessage(ClientMessageType.ACK);

        return new ServerMessage(
                ServerMessageType.getMessageType(message.substring(0, 10)),
                message.substring(10));
    }
    catch (IOException e)
    {
        throw new ClientException("Error reading message from server!", e);
    }
}

public void sendMessage(ClientMessageType message) throws ClientException
{
    this.sendMessage(message, null);
}

public void sendMessage(ClientMessageType message, String extraText) throws ClientException
{
    try
    {
        if(extraText != null)
            this.outStream.write(message.getMessage() + extraText + '\n');
        else
            this.outStream.write(message.getMessage() + '\n');

        this.outStream.flush();
    }
    catch (IOException e)
    {
        throw new ClientException("Message could not be sent to server!", e);
    }
}

}

在Intellij中运行此代码(减去android内容)时,它运行良好。但是,一旦我在Android studio中运行相同的客户端命令,它就会中断。我知道这不是防火墙或端口转发问题。每当我运行它时,LogCat会给出I/System.out: com.mchat.messagingapp.ClientException: Encountered an error while initializeing client!它似乎甚至没有到达服务器。我相信错误是在使用Android,但我找不到它在哪里打破。我感谢任何和所有帮助,并乐意根据需要提供更多信息。

0 个答案:

没有答案