首先,在我的应用程序中,我调用了该代码:
String login = e1.getText().toString();
String password = e2.getText().toString();
login = login.replace(" " , "");
password = password.replace(" " , "");
String a = FunkcjeAPI.zalogujSie(login, password);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
它应该调用FunkcjeAPI.zalogujSie()
- 该方法显示 LogCat 记录并返回到我的代码,在那里我显示其他 LogCat 记录。
所以正确的顺序是:
07-27 20:51:48.610: A/Link(876): http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=&password=&imei=
07-27 20:51:48.630: A/Link(876): {"error":"You reached daily query limit !"}
07-27 20:51:48.330: A/Uwaga !(876): null
07-27 20:51:48.335: A/Uwaga !(876): null
07-27 20:51:48.335: A/Uwaga !(876): null
07-27 20:51:48.340: A/Uwaga !(876): null
07-27 20:51:48.345: A/Uwaga !(876): null
但是我的应用程序按该顺序返回记录:
07-27 20:51:48.330: A/Uwaga !(876): null
07-27 20:51:48.335: A/Uwaga !(876): null
07-27 20:51:48.335: A/Uwaga !(876): null
07-27 20:51:48.340: A/Uwaga !(876): null
07-27 20:51:48.345: A/Uwaga !(876): null
07-27 20:51:48.610: A/Link(876): http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=&password=&imei=
07-27 20:51:48.630: A/Link(876): {"error":"You reached daily query limit !"}
zalogujSie()
功能代码:
public static String zalogujSie(final String nick, final String haslo)
{
final JSONParser jParser = new JSONParser();
new Thread(new Runnable() {
public void run() {
final String json = jParser.getJSONFromUrl("http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + nick + "&password=" + haslo + "&imei=");
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
JSONObject jObject;
try {
jObject = new JSONObject(json);
Log.wtf("Link", "http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + nick + "&password=" + haslo + "&imei=");
Log.wtf("Link", json);
String error = jObject.getString("error");
if(error == "You reached daily query limit !") { nadajWartosc("You reached daily query limit !"); }
if(error == "0") {nadajWartosc(jObject.getString("token"));}
if(error == "1") {nadajWartosc("1");}
if(error == "Invalid username") {nadajWartosc("Invalid username");}
if(error == "Invalid password") {nadajWartosc("Invalid password");}
if(error == "This user is already logged in !") {nadajWartosc("This user is already logged in !");}
} catch (JSONException e1) {
e1.printStackTrace();
}
catch (NullPointerException e)
{
e.printStackTrace();
}
}
});
}}).start();
return dozwrotu;
}
我不知道这是什么原因。现在我的应用程序无法正常工作。我试图添加wait()
方法,但它没有帮助我。
@编辑
Michael Butscher给了我一个使用with的建议。它没有帮助我。也许我的代码中有错误(但编译器没有返回任何错误)?AsyncTask
ProgressDialog
中的代码SignInActivit
:
String login = e1.getText().toString();
String password = e2.getText().toString();
login = login.replace(" " , "");
password = password.replace(" " , "");
String[] argi = {login,password};
String a = null;
a = FunkcjeAPI.zalogujSie(this, login, password);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
Log.wtf("Uwaga !", a);
if(a != "Invalid username" && a != "Invalid password" && a != "1")
t1.setText("Inserted incorrect data");
if(a == "You reached daily query limit !")
t1.setText("You reached daily query limit !");
if(a == "This user is already logged in !")
t1.setText("This user is already logged in !");
并在FunkcjeAPI
:
public static String zalogujSie(Activity aktywnosc, final String nick, final String haslo)
{
String[] argumenty = {nick, haslo};
new Logowanie(aktywnosc).execute(argumenty);
return dozwrotu;
}
public static class Logowanie extends AsyncTask<String, String, String>
{
Activity wywolujaceActivity;
private ProgressDialog dialog;
public Logowanie(Activity wywolujaceActivity) {
this.wywolujaceActivity = wywolujaceActivity;
dialog = new ProgressDialog(wywolujaceActivity);
}
protected void onPreExecute() {
this.dialog.setTitle("Please wait...");
this.dialog.setMessage("Getting data from server");
this.dialog.show();
}
@Override
protected String doInBackground(final String... argi) {
final JSONParser jParser = new JSONParser();
new Thread(new Runnable() {
public void run() {
final String json = jParser.getJSONFromUrl("http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + argi[0] + "&password=" + argi[1] + "&imei=");
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
JSONObject jObject;
try {
jObject = new JSONObject(json);
Log.wtf("Link", "http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + argi[0] + "&password=" + argi[1] + "&imei=");
Log.wtf("Link", json);
String error = jObject.getString("error");
if(error == "You reached daily query limit !") { nadajWartosc("You reached daily query limit !"); }
if(error == "0") {nadajWartosc(jObject.getString("token"));}
if(error == "1") {nadajWartosc("1");}
if(error == "Invalid username") {nadajWartosc("Invalid username");}
if(error == "Invalid password") {nadajWartosc("Invalid password");}
if(error == "This user is already logged in !") {nadajWartosc("This user is already logged in !");}
} catch (JSONException e1) {
e1.printStackTrace();
}
catch (NullPointerException e)
{
e.printStackTrace();
}
}
});
}}).start();
return dozwrotu;
}
@Override
protected void onPostExecute(String result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}