首页 > 解决方案 > 如何从低安卓版本的安卓设备连接到Php服务器

问题描述

我有一个非常奇怪的问题。几天前,我在 Playstore 中有一个名为“Central Department of Physics”的应用程序,我能够从我的颜色 X114(Android 4.4)连接到我的 php Databaste,并且还检查了带有 android 版本的三星 Galaxy 并且连接成功但现在我没有改变任何东西,但是当我连接到 php 服务器时,几天以来我得到“服务器连接失败”。

但是当我尝试从更高版本的 android 设备连接时,它连接成功......我尝试了 3-4 个设备,结果是肯定的。

那么任何人都可以帮我解决这个问题吗?

我的活动文件是

public class MainActivity extends AppCompatActivity {

    private Toolbar mainToolbar;
    private Menu mymenu;

    protected EditText username;
    private Button registerbutton;

    private EditText password;

    protected String enteredUsername;
    private ProgressBar loginProgress;

    private final String serverUrl = "https://forappinthenameofking.000webhostapp.com/index.php";

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_mainv);
        loginProgress = findViewById(R.id.login_progressv);
       registerbutton = findViewById(R.id.registerbuttonm);

        mainToolbar = (Toolbar) findViewById(R.id.mainv);
        setSupportActionBar(mainToolbar);
        getSupportActionBar().setTitle("Central Department of Physics");
        mainToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent newPostIntent = new Intent(MainActivity.this, com.nepalpolice.cdp.MainActivity.class);
                startActivity(newPostIntent);

            }
        });

        username = (EditText)findViewById(R.id.username_field);

        password = (EditText)findViewById(R.id.password_field);

        Button loginButton = (Button)findViewById(R.id.login);


        registerbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, form.class);

                startActivity(intent);


            }
        });


        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {
                loginProgress.setVisibility(View.VISIBLE);

                enteredUsername = username.getText().toString();

                String enteredPassword = password.getText().toString();

                if(enteredUsername.equals("") || enteredPassword.equals("")){

                    Toast.makeText(MainActivity.this, "Username or password must be filled", Toast.LENGTH_LONG).show();

                    return;

                }

                if(enteredUsername.length() <= 1 || enteredPassword.length() <= 1){

                    Toast.makeText(MainActivity.this, "Username or password length must be greater than one", Toast.LENGTH_LONG).show();

                    return;

                }





// request authentication with remote server4

                AsyncDataClass asyncRequestObject = new AsyncDataClass();

                asyncRequestObject.execute(serverUrl, enteredUsername, enteredPassword);

            }

        });



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        mymenu = menu;
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_refresh:
                Intent intent = new Intent(MainActivity.this, UpdateService.class);
                startService(intent);
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ImageView iv = (ImageView) inflater.inflate(R.layout.iv_refresh, null);
                Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_refresh);
                rotation.setDuration(500);
                iv.startAnimation(rotation);
                item.setActionView(iv);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }



    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 jsonResult = "";

            try {

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

                nameValuePairs.add(new BasicNameValuePair("username", params[1]));

                nameValuePairs.add(new BasicNameValuePair("password", params[2]));

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                jsonResult = inputStreamToString(response.getEntity().getContent()).toString();

            } catch (ClientProtocolException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            }

            return jsonResult;

        }

        @Override

        protected void onPreExecute() {

            super.onPreExecute();

        }

        @Override

        protected void onPostExecute(String result) {

            super.onPostExecute(result);

            System.out.println("Resulted Value: " + result);

            if(result.equals("") || result == null){

                Toast.makeText(MainActivity.this, "Server connection failed", Toast.LENGTH_LONG).show();

                return;

            }

            int jsonResult = returnParsedJsonObject(result);

            if(jsonResult == 0){

                Toast.makeText(MainActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();

                return;

            }

            if(jsonResult == 1){

                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                loginProgress.setVisibility(View.VISIBLE);

                intent.putExtra("USERNAME", enteredUsername);

                intent.putExtra("MESSAGE", "You have been successfully login");

                startActivity(intent);

            }
            loginProgress.setVisibility(View.INVISIBLE);
        }

        private StringBuilder inputStreamToString(InputStream is) {

            String rLine = "";

            StringBuilder answer = new StringBuilder();

            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            try {

                while ((rLine = br.readLine()) != null) {

                    answer.append(rLine);

                }

            } catch (IOException e) {

// TODO Auto-generated catch block

                e.printStackTrace();

            }

            return answer;

        }

    }

    private int returnParsedJsonObject(String result){

        JSONObject resultObject = null;

        int returnedResult = 0;

        try {

            resultObject = new JSONObject(result);

            returnedResult = resultObject.getInt("success");

        } catch (JSONException e) {

            e.printStackTrace();

        }

        return returnedResult;

    }

}

标签: javaphpandroidmysql

解决方案


推荐阅读