0

这是我获取 JSON 数组的代码:

    public void download_vse_igralce(View view)
{
    mylist.clear();  //sem dal v oklepaje
    String result = "";
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("year","1980"));
     InputStream is = null;
    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://gregor-apps.com/ubs/pokazi_vse_igralce.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
             is = entity.getContent();
    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();

            result=sb.toString();
    }catch(Exception e){

            Log.e("log_tag", "Error converting result "+e.toString());
    }

    //parse json data
    try{
        {
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    json_data = jArray.getJSONObject(i);


                    Log.i("log_tag","id: "+json_data.getInt("id")+
                            ", ime: "+json_data.getString("ime")+
                            ", priimek: "+json_data.getString("priimek")+
                            ", pozicija: "+json_data.getString("pozicija")
                            );

                            HashMap<String, String> map = new HashMap<String, String>();

                    map.put("id",  json_data.getString("id"));                    
                    map.put("firstname",  json_data.getString("ime"));                     
                    map.put("lastname",  json_data.getString("priimek"));   
                    map.put("position",  json_data.getString("pozicija"));  
                    map.put("height",  json_data.getString("height"));  
                    map.put("weight",  json_data.getString("weight"));  
                    map.put("hometown",  json_data.getString("hometown"));  
                    map.put("birthdate",  json_data.getString("birthdate"));    
                    map.put("jersey",  json_data.getString("number"));  
                    map.put("picture",  json_data.getString("slika"));
                    mylist.add(map);

            }
            downloadani_igralci=1;


    }

    }catch(JSONException e){
        downloadani_igralci=0;
            Log.e("log_tag", "Error parsing data "+e.toString());
    } 
    settings(view);
}

一般来说,这段代码效果很好,它会根据我的需要返回 Mylist。但有时,当我调用我的函数 download_vse_igralce() 时,应用程序会停止反应(在大多数情况下,当我的互联网连接较弱时)。我在网上搜索了异步线程(或类似的东西),但我不知道如何在这段代码中实现它。我有一种方法可以让用户在加载时看到进度条?

4

4 回答 4

0

我认为主要问题是您有三个单独的 try/catch 块,但是一旦您实际捕获到异常,您就不会停止处理。例如,您首先进行 POST,如果失败,您仍然尝试读取输出。相反,您应该return;在 catch 块中停止处理。

AsyncTasks 也很好,从 4.0 开始需要,但不是解决问题的方法。你也应该调查一下。

于 2012-10-31T13:15:33.760 回答
0

像这样的东西:

public void download_vse_igralce(View view){
  mylist.clear(); 
  JSONParserTask task = new JSONParserTask();
  task.execute();
}

private class JSONParserTask extends AsyncTask<Void, Void, Void> {

  protected Void doInBackground( Void... ignoredParams ) {
    String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
 InputStream is = null;
//http post
try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://gregor-apps.com/ubs/pokazi_vse_igralce.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
         is = entity.getContent();
}catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
        }
        is.close();

        result=sb.toString();
}catch(Exception e){

        Log.e("log_tag", "Error converting result "+e.toString());
}

//parse json data
try{
    {
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);


                Log.i("log_tag","id: "+json_data.getInt("id")+
                        ", ime: "+json_data.getString("ime")+
                        ", priimek: "+json_data.getString("priimek")+
                        ", pozicija: "+json_data.getString("pozicija")
                        );

                        HashMap<String, String> map = new HashMap<String, String>();

                map.put("id",  json_data.getString("id"));                    
                map.put("firstname",  json_data.getString("ime"));                     
                map.put("lastname",  json_data.getString("priimek"));   
                map.put("position",  json_data.getString("pozicija"));  
                map.put("height",  json_data.getString("height"));  
                map.put("weight",  json_data.getString("weight"));  
                map.put("hometown",  json_data.getString("hometown"));  
                map.put("birthdate",  json_data.getString("birthdate"));    
                map.put("jersey",  json_data.getString("number"));  
                map.put("picture",  json_data.getString("slika"));
                mylist.add(map);

        }
        downloadani_igralci=1;


}

}catch(JSONException e){
    downloadani_igralci=0;
        Log.e("log_tag", "Error parsing data "+e.toString());
} 
  }

  protected void onPostExecute( Void array ) {
    settings(view);
  }

}
于 2012-10-31T13:19:23.927 回答
0

我认为这将是因为互联网连接。有时您的应用程序无法正确下载 JSON 内容,因此当它尝试解析时,它会进入错误状态,并且您的应用程序崩溃是因为 try-catch 样式不正确

于 2012-10-31T13:58:55.533 回答
0

“尽管连接良好,但 asynchTask 并不能解决网络不可达的问题。”

您可以通过添加来修复它:

httpclient.getConnectionManager().shutdown();

这将解决“保持活动”连接的问题。

于 2014-04-12T19:37:20.317 回答