所以我在Android代码下方有效。但只有在下面的代码中指出的那一点。 HTTPPost从Web服务器获取一个JSON字符串。在postExecute中,我检查返回的JSONObject的键值" success"。如果它是1然后做一些事情。否则做别的事情。然而,"做其他事情"不管用。不知道为什么不。应用程序不会崩溃,只需返回屏幕即可。
此外,我对以下更好的代码的任何提示也很感激,因为我是新手。
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Registreren extends ActionBarActivity {
EditText inputName;
EditText inputEmail;
EditText inputPass;
TextView textError;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registreren);
// Importing all assets like buttons, text fields
inputName = (EditText) findViewById(R.id.register_name);
inputEmail = (EditText) findViewById(R.id.register_email);
inputPass = (EditText) findViewById(R.id.register_pass);
textError = (TextView) findViewById(R.id.register_error);
}
public void registreer (View v) {
String serverURL = "http://www.somesite.com";
new LongOperation().execute(serverURL);
}
public void openLogin (View view) {
Intent intent = new Intent(this, Login.class);
startActivity(intent);
}
private class LongOperation extends AsyncTask<String, Void, JSONObject> {
private ProgressDialog Dialog = new ProgressDialog(Registreren.this);
protected void onPreExecute() {
Dialog.setMessage("Een moment geduld aub...");
Dialog.show();
}
protected JSONObject doInBackground(String... urls) {
String naam = inputName.getText().toString();
String email = inputEmail.getText().toString();
String pass = inputPass.getText().toString();
JSONObject finalResult = null;
HttpParams httpParameters = new BasicHttpParams();
//Set the connection timeout and socket timeout parameters (ms)
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters,5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost("http://www.somesite.com");
try {
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("username", naam));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity =response.getEntity();
if (responseEntity != null){
BufferedReader reader = new BufferedReader(
new InputStreamReader(responseEntity.getContent(),"UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line ; (line = reader.readLine()) !=null;) {
builder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(builder.toString());
try{
finalResult = new JSONObject(tokener);
} catch (JSONException e) {Toast.makeText(getApplicationContext(), "Data error.Probeer het later nog eens", Toast.LENGTH_LONG).show(); }
} else {
Toast.makeText(getApplicationContext(), "Geen response", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return finalResult;
}
protected void onPostExecute(JSONObject result) {
Integer succes;
String naam;
String pass;
String email;
try {
succes = result.getInt("succes");
naam = result.getString("username");
pass = result.getString("pass");
email = result.getString("email");
if(succes==1){
SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.clear();
edit.putString("username", naam);
edit.putString("password", pass);
edit.apply();
Intent intent = new Intent (getApplicationContext(),Overzicht.class);
startActivity(intent);
}
else {//in debug i can come until this line
if(naam.equals("user_bestaat")){//this line and below is ignored
TextView t = (TextView)findViewById(R.id.register_error);
t.setText("Gebruikersnaam bestaat al");
}
if(email == "email_bestaat"){
TextView t = (TextView)findViewById(R.id.register_error);
t.setText("Email is al geregistreerd");
}
}
} catch (JSONException e){Toast.makeText(getApplicationContext(), "Data error.Probeer het later nog eens", Toast.LENGTH_LONG).show(); }
Dialog.dismiss();
//SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
// System.out.println("value of the userdertails ==========>"+ userDetails);
//String Uname="";
//String pass= "";
// Uname = userDetails.getString("username", "");
//pass = userDetails.getString("password", "");
}
}
@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_overzicht, 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);
}
}
答案 0 :(得分:0)
与==
比较意味着通过参考进行比较,而不是这里的情况。您想要比较两个字符串的值。
更改为
if(email.equals("email_bestaat")){
TextView t = (TextView)findViewById(R.id.register_error);
t.setText("Email is al geregistreerd");
}