我是异步任务的新手,并尝试创建登录UI。当我使用有效凭据从服务器发出请求时,一切正常。当凭据不正确时,我使用一组“if”语句来检查响应的错误代码是什么并打印相应的消息。对于响应为空的情况,我也有这样的消息。我可以从服务器的部分看到所有响应都包含状态代码,即使凭据无效也是如此。此外,我可以通过在代码中使用检查点来查看状态代码。但是当我尝试从无效凭据获得的响应中提取状态代码时,唯一有效的“if”语句是检查响应是否等于“”的语句。其他“如果”陈述的任何条件都不符合要求。这是我的代码:
public String GET(String u) {
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader;
StringBuilder stringBuilder;
String line;
String jsonString = "";
try {
URL url = new URL(u);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + '\n');
}
jsonString = stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
httpURLConnection.disconnect();
}
return jsonString;
}
public void ProcessResponse(String response) {
if(response!="") {
try {
json = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
try {
status = json.getInt("status_code");
if (status == 500) {
Toast.makeText(this, "Internal server error! Please repeat action!", Toast.LENGTH_SHORT).show();
} else if(status == 401) {
Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show();
} else if(status == 400) {
Toast.makeText(this, "Bad Request! Please repeat action!", Toast.LENGTH_SHORT).show();
} else if(status == 200) {
try {
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("base_url", baseURL);
editor.putInt("store_id", json.getInt("store_id"));
editor.putInt("pin", json.getInt("pin_number"));
editor.putInt("delete_table",json.getInt("delele_table_id"));
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(getApplicationContext(), APICall.class);
intent.putExtra("Action","Main Menu");
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(LoginActivity.this, "Problem encountered!", Toast.LENGTH_SHORT).show();
}
}
private class ServerCall extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String url = params[0];
return GET(url);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
ProcessResponse(s);
}
}
这些函数在代码的另一部分中使用执行函数调用,通常在AsyncTask中完成。
提前感谢您的帮助!
编辑:该函数调用如下:
public class LoginActivity extends AppCompatActivity {
public static final String MY_PREFS_NAME = "Shared Preferences";
String url = "";
String baseURL = "";
int status = 0;
JSONObject json = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button login = (Button) findViewById(R.id.login_button);
CheckBox mode = (CheckBox) findViewById(R.id.login_development_mode);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText developerURL = (EditText) findViewById(R.id.base_url);
EditText store = (EditText) findViewById(R.id.store_id_field);
EditText pin = (EditText) findViewById(R.id.pin_field);
baseURL = developerURL.getText().toString();
url = baseURL + "/api/auth/check?store_id="+store.getText().toString()+
"&pin_number="+pin.getText().toString();
ServerCall loginCall = new ServerCall();
loginCall.execute(url);
}
});
/*more code, including the AsyncTask mentioned above*/
}
答案 0 :(得分:0)
我找到了问题的答案。谢谢大家的回复。我更改了GET和processResponse方法如下:
public String GET(String u) {
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader;
StringBuilder stringBuilder;
String line;
String jsonString = "";
int code = -1;
try {
URL url = new URL(u);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
code = httpURLConnection.getResponseCode();
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + '\n');
}
jsonString = stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
status = code;
return "";
} finally {
httpURLConnection.disconnect();
}
return jsonString;
}
public void ProcessResponse(String response) {
if (status == 500) {
Toast.makeText(this, "Internal server error! Please repeat action!", Toast.LENGTH_SHORT).show();
} else if(status == 401) {
Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show();
} else if(status == 400) {
Toast.makeText(this, "Bad Request! Please repeat action!", Toast.LENGTH_SHORT).show();
} else if(status == 200) {
if(response!="") {
try {
json = new JSONObject(response);
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("base_url", baseURL);
editor.putInt("store_id", json.getInt("store_id"));
editor.putInt("cell_phone", json.getInt("cell_phone"));
editor.putInt("pin", json.getInt("pin_number"));
editor.putInt("delete_table", json.getInt("delele_table_id"));
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(getApplicationContext(), APICall.class);
intent.putExtra("Action","Main Menu");
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "Problem encountered!", Toast.LENGTH_SHORT).show();
}
}
}
我不确定我使用httpURLConnection的方式是否最有效,或者我的代码是愚蠢的,但是它有效,所以如果有人有任何改进建议,请随意!再次感谢大家!