如何从低版本的android设备连接到php服务器

xlpyo6sf  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(223)

我有个很奇怪的问题。几天前,我在playstore中有一个名为“中央物理部”的应用程序,我可以从我的颜色x114连接到我的php数据库,这是android 4.4,还与三星galaxy的android版本samme进行了检查,连接成功,但现在我什么都没做,但当我连接到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;

    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题