从Web服务中捕获响应

时间:2012-04-30 01:39:41

标签: android httpresponse

我为我的应用程序提供Web服务,我的回答是“成功”和“失败” 我希望将我的服务回复到我的申请中 如果我的回答“成功”我做了什么,但如果“失败”没有做任何事情 我尝试:
 (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;

}

谢谢你的反馈:)

1 个答案:

答案 0 :(得分:0)

我会做这样的事情。

当HttpResponse回来时,您只需要检查您是否收到回复以及回复是否为HttpStatus.SC_OK(200)。得到之后,解析实体并检查“SUCCESS”或“FAILURE”。在您的代码中,您将实体对象与“SUCCESS”或“FAILURE”进行比较。

请记住我的缩进。我刚刚更改了原始发布的代码。

  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 (response.getStatusLine().getStatusCode() == 200) {
            String res = request(response);
            if(res.equalsIgnoreCase("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(res.equalsIgnoreCase("FAILURE")){
                Toast.makeText(this, "gagal", Toast.LENGTH_LONG).show();
                System.out.println("GAGAL");
            }

        }/*else if(response.getStatusLine().getStatusCode() == 408){
            Toast.makeText(this, "Request Timeout", Toast.LENGTH_LONG).show();
        }*/else {
            Toast.makeText(this, "Inisialisasi "+response.getStatusLine(), 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;

}
}