2
public String getBrotherHood() throws Exception{        
    client = new DefaultHttpClient();
    get = new HttpGet(uri);
    res = client.execute(get);
    sl = res.getStatusLine();
    sCode = sl.getStatusCode();
    if(sCode==200)
    {
        try{
            reader = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
            readBuffer = new StringBuffer();
            while((nl = reader.readLine())!=null){
                readBuffer.append(nl);
            }
            reader.close();
        }finally{
            if(reader !=null)
            {
                try{
                    reader.close();
                }catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }
    }
    return readBuffer.toString();       
  }
}

我有这段代码,这是正确的编写方式,还是我需要遵循任何编码模式或标准?

请给我一些建议,因为我不是 Android Coding。

更新:

public class JSONData {

public ArrayList<String> getBrotherHoodJSON() throws JSONException,IOException,Exception{
    ArrayList<String> item = new ArrayList<String>();
    JSONArray jA = new JSONArray(getBrotherHood());

    for(int i=0; i<jA.length(); i++)
    {
        JSONObject jO = jA.getJSONObject(i);
        String n = jO.getString("name");
        item.add(n);
        Log.i("JsonData:",jO.getString("name"));
    }

    return item;
}   

public String getBrotherHood() throws Exception{    
    BufferedReader in = null;
    String data= null;
    HttpClient client = new DefaultHttpClient();
    URI uri = new URI("http://fahidmohammad.in/demo/Android/api.php?user=fah");
    HttpGet get = new HttpGet();
    get.setURI(uri);
    HttpResponse res = client.execute(get);
    StatusLine sl = res.getStatusLine();
    int sCode = sl.getStatusCode();
    if(sCode==200)
    {
        try{
            in = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
            StringBuffer sb = new StringBuffer();
            String nl;
            while((nl = in.readLine())!=null){
                sb.append(nl);
            }
            in.close();
            data = sb.toString();
            Log.i("Raw Data:",data);
            return data;
        }finally{
            if(in !=null)
            {
                try{
                    in.close();
                    return data;
                }catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }
    }
    return data;        
}

这是相同代码的更新版本。处理了所有提到的问题。

而且它的工作就像一个魅力,但不知道它的稳定性。

4

3 回答 3

1

您显然已经在您的方法之外声明了所有变量,即使它们似乎只在此方法中使用。这是没有意义的。它可以防止多个对象被垃圾收集。您最好将声明移到方法中。

而且声明throws Exception也没有多大意义。最好是声明一个可能发生的特定异常,或者将所有不太可能的异常转换为RuntimeException不需要声明它们。

于 2012-06-16T10:46:56.637 回答
0

有一个默认的代码格式化程序可用 eclipse。编写代码后,只需键入"Control + Shift + F"。您的 android 代码将自动格式化。

于 2012-06-16T10:40:50.617 回答
0

更重要的一点是重新升级 readBuffer 变量

在函数的最后一个返回 readBuffer.toString() 并且仅当状态为 200 时才对其进行初始化。但如果状态不是 200,那么它将为空(假设声明不可见),因此 null.toString() 将思想异常。

于 2012-06-16T11:08:19.523 回答