java - 如何从低安卓版本的安卓设备连接到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;
}
}
解决方案
推荐阅读
- javascript - Mongoose(或 MongoDB)中的 TransientTransactionError 是什么?
- apache-spark - 杀死 oozie coordinator 不会杀死后续的 spark 作业
- c++ - WinAPI LoadImage 到按钮有边框但 LoadBitmap 没有
- python - 在 TF 服务或 TF 图的一部分中查找外部数据库
- jquery - jquery元素的茉莉花测试
- r - 从 .asp 中抓取表格
- java - 这个 docRoot 变量的位置是什么?
- python - 如何识别 Python 中的过拟合和欠拟合
- html - 仅在 Safari 上错误地显示超出包含 div/段落右边界的文本
- java - 在 TextButton 上测试事件处理程序