我有在android studio中使用php sql进行登录活动的代码,代码如下:-
public class MainActivity extends ActionBarActivity {
protected EditText username;
private EditText password;
protected String enteredUsername;
private final String serverUrl = "http://192.168.0.103/androidlogin/index.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.username_field);
password = (EditText)findViewById(R.id.password_field);
Button loginButton = (Button)findViewById(R.id.login);
Button registerButton = (Button)findViewById(R.id.register_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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);
}
});
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
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);
intent.putExtra("USERNAME", enteredUsername);
intent.putExtra("MESSAGE", "You have been successfully login");
startActivity(intent);
}
}
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;
}
}
有了21个sdk版本,它工作得非常完美,但当我尝试将gradle文件更改为25版本时,在http连接代码下出现了许多红线
那么解决方案是什么?我如何将sdk升级到25而不出现任何问题?非常感谢
您必须用HttpURLConnection替换HttpClient。查看此链接了解更多信息:Android Gradle Apache HttpClient不存在?
HttpCLient已在SDK 23中删除。您应该手动添加Apache Http库:
android {
useLibrary 'org.apache.http.legacy'
}
也许我需要添加appcompat_v_7,我尝试过,但每次它都给我很多错误
在我更改了本地存储库上的一些文件后,我想将这些文件提交给我的GitHub代表。每次出现这个错误,我都不知道如何修复它。我试图卸载VSCode和Git在我的PC上,什么都没有改变。 错误:无法生成gpg2:没有这样的文件或目录错误:gpg未能签署数据致命:写入提交对象失败
它在使用但是我在尝试登录:by browser complains但是我在控制台中没有错误。不管怎样...我的问题是:如何将SpringBoot更新到2.1.0.build-snapshot?
从导出数据时出现错误。我正在使用 错误消息是: 我是MySQL的新手。
我有一个问题,在Visual Studio代码中,即使启用了编辑器,我也看不到用于合并冲突的工具栏。codeLens。 知道它还能是什么吗?我试图重新启动VS Code,禁用/启用editor.code镜头。我甚至安装/卸载了Gitlen-Git增压。 提前谢谢你。 我的输入参考来自:接受VS代码中未出现的传入更改