【问题标题】:Create an array from a remote XML for android从远程 XML 为 android 创建一个数组
【发布时间】:2011-06-20 17:25:16
【问题描述】:

我有一个要用于填充列表视图的远程 XML 文件。

我目前已将应用程序设置为从本地数组创建列表视图。如何使用在线存储的 XML 文件填充数组?该数组当前位于strings.xml中

public class ArchiveListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
            R.array.archivetitle, R.layout.archiveitem));

    final String[] links = getResources().getStringArray(R.array.archivelinks);

    getListView().setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String content = links[position];
            Intent showContent = new Intent(getApplicationContext(),
                    ArchiveViewerActivity.class);
            showContent.setData(Uri.parse(content));
            startActivity(showContent);
        }
    });
}
}

【问题讨论】:

    标签: android xml arrays sax


    【解决方案1】:

    您可能可以通过以下提到的步骤来做到这一点:

    1> 准备 xml 所在的请求 URI。

    prepareRequestUrl();
    

    2> 从网络服务器获取响应:

    /** 
     * fetch the response for the request url
     * @param request url string
     * @return InputStream
     */
    public InputStream getResponse(String reqUrl) throws AppException {
    
        URL                 url             =   null;
        URLConnection       connection      =   null;
        HttpURLConnection   httpConnection  =   null;
        int                 reponseCode     =   0;
        InputStream         inputStream     =   null;
    
        try {
    
            url = new URL(reqUrl);
    
            connection = url.openConnection();
    
            httpConnection = (HttpURLConnection) connection;
    
            reponseCode = httpConnection.getResponseCode();
    
        } catch (MalformedURLException e) {
    
        } catch (IOException e) {
    
        }
    
        if (reponseCode == HttpURLConnection.HTTP_OK) {
            try {
    
                inputStream = httpConnection.getInputStream();
    
            } catch (IOException e) {
    
            }
        }
        else    {
            throw new AppException(AppConstants.HTTP_RESPONSE_FAILURE);
        }
    
        return inputStream;
    }
    

    3>解析从服务器接收到的输入流xml:

                       inputStream = super.getResponse(requestUrl);             
            result= xmlParser.parseList(inputStream);
    

    4> 在列表视图中显示相应的结果。

    注意:始终建议使用异步任务来执行任何网络操作。这里在这种情况下调用 we-server。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多