【问题标题】:catch response from web service捕获来自 Web 服务的响应
【发布时间】:2012-04-30 09:55:19
【问题描述】:

我为我的应用程序提供网络服务,我的响应是“成功”和“失败”
我想将我的服务的响应捕获到我的应用程序中
如果我的回答“成功”我会做某事,但如果“失败”什么都不做
我尝试使用:
(request(response) != null && response.getStatusLine().getStatusCode() == 200)
不工作,因为它只是读取网络状态 oknot..
我该怎么做??
这是我的完整代码:

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;

}

感谢您的反馈:)

【问题讨论】:

  • 看看答案here有没有帮助。

标签: android httpresponse


【解决方案1】:

我会做这样的事情。

当 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;

}
}

【讨论】:

  • 我尝试像你的代码一样,但它仍然不起作用..我的应用程序没有做任何事情..你能给出另一个建议吗?谢谢你:) @PH7
  • 你有没有得到任何错误?告诉我你得到了什么。这将有助于分析问题。
  • 我没有收到任何错误..但我的应用程序没有做任何事情..在 logCat 我收到这样的消息04-30 09:26:52.669: INFO/global(2388): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 我该怎么办?谢谢 :)
  • 我的应用程序没有做任何事情,而如果我的服务响应 SUCCESS..请帮助我..谢谢 :) @PH7
  • 解决了!!部分String res = request(respones);我用String res = UntityUtils.toString(response.getEntity());修改了它的工作!!:)谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 2016-02-18
相关资源
最近更新 更多