Android登录活动无法从数据库中获取(Android Login Activity not fetching from database)

所以从上一个问题我添加了一些新的依赖项到我的Gradle属性以允许HttpGetRequest命令等。但是现在我的代码中还有其他错误,我认为我添加的新依赖项与JSON语句不兼容。 你能看一看,看看这些依赖是否破坏了我的代码?

Gradle依赖

dependencies { compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1' compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' }

我的LoginActivity代码:

package com.example.george.youdecide; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import cz.msebera.android.httpclient.NameValuePair; import cz.msebera.android.httpclient.message.BasicNameValuePair; public class LoginActivity extends Activity implements OnClickListener { // Initializing variables private EditText user, pass; private ImageView mSubmit, mRegister; // Progress Dialog private ProgressDialog pDialog; // JSON parser class JSONParser jsonParser = new JSONParser(); //Localhost URL stuff goes here private static final String LOGIN_URL = "http://172.18.5.215/webservice/login.php"; //JSON element ids from repsonse of php script: private static final String TAG_SUCCESS = "success"; private static final String TAG_MESSAGE = "message"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); //setup input fields user = (EditText)findViewById(R.id.usernameLogin); pass = (EditText)findViewById(R.id.passwordLogin); //setup buttons mSubmit = (ImageView) findViewById(R.id.loginButton); //register listeners mSubmit.setOnClickListener((OnClickListener) this); } @Override public void onClick(View v) { // determine which button was pressed: switch (v.getId()) { case R.id.loginButton: new AttemptLogin().execute(); break; //case R.id.register: // Intent i = new Intent(this, Register.class); //startActivity(i); // break; default: break; } } class AttemptLogin extends AsyncTask<String, String, String> { /** * Before starting background thread Show Progress Dialog * */ boolean failure = false; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(LoginActivity.this); pDialog.setMessage("Attempting login..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected String doInBackground(String... args) { // TODO Auto-generated method stub // Check for success tag //user.getText method error here int success; String username = user.getText().toString(); String password = pass.getText().toString(); try { // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", username)); params.add(new BasicNameValuePair("password", password)); Log.d("request!", "starting"); // getting product details by making HTTP request //Parameter errors here JSONObject json = jsonParser.getJSONFromUrl( LOGIN_URL, "POST", params); // check your log for json response Log.d("Login attempt", json.toString()); // json success tag success = json.getInt(TAG_SUCCESS); if (success == 1) { Log.d("Login Successful!", json.toString()); Intent i = new Intent(LoginActivity.this, MainActivity.class); finish(); startActivity(i); return json.getString(TAG_MESSAGE); }else{ Log.d("Login Failure!", json.getString(TAG_MESSAGE)); return json.getString(TAG_MESSAGE); } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * Get rid of progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once product deleted pDialog.dismiss(); if (file_url != null){ Toast.makeText(LoginActivity.this, file_url, Toast.LENGTH_LONG).show(); } } } }

更改依赖项后,我仍然会遇到代码的这些部分错误。

int success; String username = user.getText().toString(); String password = pass.getText().toString(); try {

和这里

Log.d("request!", "starting"); // getting product details by making HTTP request JSONObject json = jsonParser.makeHttpRequest( LOGIN_URL, "POST", params);

So from a previous question I have added some new dependencies to my Gradle properties to allow HttpGetRequest commands etc. However now there are further errors in my code which I presume mean that the new dependency I have added is not compatible with the JSON statements. Can you please have a look through and see whether it is these dependencies that have broken my code?

Gradle Dependencies

dependencies { compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1' compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' }

And my LoginActivity code:

package com.example.george.youdecide; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import cz.msebera.android.httpclient.NameValuePair; import cz.msebera.android.httpclient.message.BasicNameValuePair; public class LoginActivity extends Activity implements OnClickListener { // Initializing variables private EditText user, pass; private ImageView mSubmit, mRegister; // Progress Dialog private ProgressDialog pDialog; // JSON parser class JSONParser jsonParser = new JSONParser(); //Localhost URL stuff goes here private static final String LOGIN_URL = "http://172.18.5.215/webservice/login.php"; //JSON element ids from repsonse of php script: private static final String TAG_SUCCESS = "success"; private static final String TAG_MESSAGE = "message"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); //setup input fields user = (EditText)findViewById(R.id.usernameLogin); pass = (EditText)findViewById(R.id.passwordLogin); //setup buttons mSubmit = (ImageView) findViewById(R.id.loginButton); //register listeners mSubmit.setOnClickListener((OnClickListener) this); } @Override public void onClick(View v) { // determine which button was pressed: switch (v.getId()) { case R.id.loginButton: new AttemptLogin().execute(); break; //case R.id.register: // Intent i = new Intent(this, Register.class); //startActivity(i); // break; default: break; } } class AttemptLogin extends AsyncTask<String, String, String> { /** * Before starting background thread Show Progress Dialog * */ boolean failure = false; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(LoginActivity.this); pDialog.setMessage("Attempting login..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected String doInBackground(String... args) { // TODO Auto-generated method stub // Check for success tag //user.getText method error here int success; String username = user.getText().toString(); String password = pass.getText().toString(); try { // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", username)); params.add(new BasicNameValuePair("password", password)); Log.d("request!", "starting"); // getting product details by making HTTP request //Parameter errors here JSONObject json = jsonParser.getJSONFromUrl( LOGIN_URL, "POST", params); // check your log for json response Log.d("Login attempt", json.toString()); // json success tag success = json.getInt(TAG_SUCCESS); if (success == 1) { Log.d("Login Successful!", json.toString()); Intent i = new Intent(LoginActivity.this, MainActivity.class); finish(); startActivity(i); return json.getString(TAG_MESSAGE); }else{ Log.d("Login Failure!", json.getString(TAG_MESSAGE)); return json.getString(TAG_MESSAGE); } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * Get rid of progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once product deleted pDialog.dismiss(); if (file_url != null){ Toast.makeText(LoginActivity.this, file_url, Toast.LENGTH_LONG).show(); } } } }

After changing the dependencies I still get errors with these parts of the code.

int success; String username = user.getText().toString(); String password = pass.getText().toString(); try {

and here

Log.d("request!", "starting"); // getting product details by making HTTP request JSONObject json = jsonParser.makeHttpRequest( LOGIN_URL, "POST", params);

最满意答案

使用:

android { ... useLibrary 'org.apache.http.legacy' ...

代替:

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'

Use:

android { ... useLibrary 'org.apache.http.legacy' ...

Instead of:

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'

更多推荐