Ich versuche, eine Anmeldung Anwendung, die Daten aus einer Online-Datenbank erhält. Ich habe ein Tutorial im Internet gefunden, aber die meisten Methoden sind veraltet. Also würde ich gerne wissen, wie ich alternative Methoden für die veralteten finden kann.Alternative Methoden für HttpClient, HttpPost, HttpResponse, HttpEntity usw. in Android
Nachdem ich den Code auf dem Tutorial zu meinem Projekt angepasst, habe ich diesen Code auf meinem MainActivity:
public class MainActivity extends AppCompatActivity {
Button b1;
EditText ed1,ed2;
public static final String USER_NAME = "USERNAME";
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText) findViewById(R.id.editText2);
}
public void invokeLogin(View view){
username = ed1.getText().toString();
password = ed2.getText().toString();
login(username,password);
}
private void login(final String username,String password){
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this,"Please wait","Loading...");
}
@Override
protected String doInBackground(String... params){
String uname = params[0];
String pass = params[1];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username",uname));
nameValuePairs.add(new BasicNameValuePair("password",pass));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://example.com/login.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolExeption e){
e.printStackTrace();
} catch (UnsupportedEncodingException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return result;
}
}
}
@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);
}
}
Also das ich erhalte für folgende ones „Kann Methode/Symbol nicht lösen“:
NameValuePair
BasicNameValuePair
HttpClient
DefaultHttpClient
HttpPost
httpPost.setEntity
UrlEncondedFormEntity
HttpResponse
httpClient.execute
HttpEntity
response.getEntity
entity.getContent
BufferReader
reader.readLine
ClientProtocolExeption
e.printStackTrace
Ich bin ziemlich neu in Android-Entwicklung und ich bin hier verloren.
Ich habe versucht, diese zu ersetzen:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username",uname));
nameValuePairs.add(new BasicNameValuePair("password",pass));
Damit (bin ich richtig):
HashMap<String,String> nameValuePairs = new HashMap<>();
nameValuePairs.put("username",params[0]);
nameValuePairs.put("password",params[1]);
Sie sollten die Klasse ['HttpURLConnection'] (http://developer.android.com/reference/java/net/HttpURLConnection.html) für Netzwerkanforderungen verwenden. – NoChinDeluxe