我为我的应用程序创建了一个 Web 服务,我的响应是“SUCCESS”和“FAILURE”
,如果我的响应“SUCCESS”我做了某事,我想将我的服务的响应捕获到我的应用程序
中,但是如果“FAILURE”什么都不做
我尝试:不
(request(response) != null && response.getStatusLine().getStatusCode() == 200)
工作,因为它只是读取网络状态是否正常..我
该怎么做?
这是我的完整代码:
public void getRequest(String Url) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
//Toast.makeText(this, "Tambah Data " + request(response) + " ",Toast.LENGTH_SHORT).show();
if (request(response) != null && response.getStatusLine().getStatusCode() == 200) {
if(response.getEntity().equals("SUCCESS")){
Toast.makeText(this, "Inisialisasi sukses", Toast.LENGTH_LONG).show();
/*helper.insertUser(noImei.getText().toString(),user.getText().toString(),password.getText().toString());
startActivity(new Intent(UserForm.this,MenuUtama.class));
user.setText("");
password.setText("");*/
System.out.println("sukses");
}else if(response.getEntity().equals("FAILURE")){
Toast.makeText(this, "gagal", Toast.LENGTH_LONG).show();
System.out.println("GAGAL");
}
}/*else if(request(response) != null && response.getStatusLine().getStatusCode() == 408){
Toast.makeText(this, "Request Timeout", Toast.LENGTH_LONG).show();
}*/else if(request(response) != null && response.getStatusLine().equals("FAILURE"))
{
Toast.makeText(this, "Inisialisasi "+request(response), Toast.LENGTH_LONG).show();
return;
}
} catch (Exception ex) {
//Toast.makeText(this, "Tambah Data Gagal !", Toast.LENGTH_SHORT).show();
}
}
public static String request(HttpResponse response) {
String result = "";
try {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
in.close();
result = str.toString();
} catch (Exception ex) {
result = "Error";
}
return result;
}
谢谢你的反馈:)