连接到服务器失败,但没有堆栈跟踪错误,虽然还有其他我不明白的错误,

connection to server failed, but no stack trace error, although there are other errors which i dont understand,

我正在尝试让应用程序连接到本地主机以进行登录和注册,但到目前为止还没有成功。该应用程序在模拟器上运行正常,但在我的 phone 上也一直拒绝连接,堆栈跟踪显示连接超时,而在模拟器上它不打印。

所有其他异常都是我不熟悉的,因为我是 android 开发的新手

注册activity

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Register extends BaseActivity {

    protected EditText username;
    private EditText password;
    private EditText email;
    protected String enteredUsername;
    private final String serverUrl = "connection to server";



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


        username = (EditText)findViewById(R.id.regName);
        password = (EditText)findViewById(R.id.regpassword);
        email = (EditText)findViewById(R.id.regEmail);
        Button regsubmit = (Button)findViewById(R.id.buttonregister);

        regsubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enteredUsername = username.getText().toString();
                String enteredPassword = password.getText().toString();
                String enteredEmail = email.getText().toString();

                if(enteredUsername.equals("") || enteredPassword.equals("") || enteredEmail.equals("")){
                    Toast.makeText(Register.this, "USERNAME OR PASSWORD REQUIRED", Toast.LENGTH_LONG).show();
                    return;
                }
                if(enteredUsername.length() <= 1 || enteredPassword.length() <= 1){
                    Toast.makeText(Register.this, "USERNAME OR PASSWORD MUST BE MORE THEN ONE CHARACTER", Toast.LENGTH_LONG).show();
                    return;
                }
                // request authentication with remote server4
                AsyncDataClass asyncRequestObject = new AsyncDataClass();
                asyncRequestObject.execute(serverUrl, enteredUsername, enteredPassword, enteredEmail);

            }
        });


    }


    private class AsyncDataClass extends AsyncTask<String , Void , String> {


        @Override
        protected String doInBackground(String... params) {

            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
            HttpConnectionParams.setSoTimeout(httpParameters, 5000);

            HttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpPost httpPost = new HttpPost(params[0]);

            String result = "";
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("username", params[1]));
                nameValuePairs.add(new BasicNameValuePair("password", params[2]));
                nameValuePairs.add(new BasicNameValuePair("email", params[3]));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));



                HttpResponse response = httpClient.execute(httpPost);
                result = inputStreamToString(response.getEntity().getContent()).toString();
                System.out.println("Returned Json object " + result.toString());


            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



            return result;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            System.out.println("result Value: " + result);
            if(result.equals("") || result == null){
                Toast.makeText(Register.this, "CONNECTION TO SERVER FAILED", Toast.LENGTH_LONG).show();
                return;
            }
            int jsonResult = ParsedJsonObject(result);
            if(jsonResult == 0){
                Toast.makeText(Register.this, " INVALID EMAIL OR PASSWORD", Toast.LENGTH_LONG).show();
                return;
            }
            if(jsonResult == 1){
                Intent intent = new Intent(Register.this, Login.class);
                intent.putExtra("USERNAME", enteredUsername);
                intent.putExtra("MESSAGE", "YOUR REGISTRATION HAS BEEN SUCCESSFULL");
                startActivity(intent);

            }
        }
    }



    private StringBuilder inputStreamToString(InputStream content) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(content));
        try {
            while ((rLine = br.readLine()) != null) {
                answer.append(rLine);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return answer;

    }


    private int ParsedJsonObject(String result) {

        JSONObject resultObject = null;
        int returnedResult = 0;
        try {
            resultObject = new JSONObject(result);
            returnedResult = resultObject.getInt("SUCCESS");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return returnedResult;
    }
}

StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users.

Source.

由于您收到这条违反严格模式的消息:

StrictMode policy violation

可以安全地假设您是偶然犯了一个错误,如果您开始研究 StrictMode 的一般情况和具体情况,就可以检测到这个错误。