在Android中格式化数据库中的输入流数据



我正在从一个表中读取数据,因为所有行都是一次提取并传递给android客户端的。

现在,我需要在阅读这些数据时对其进行格式化,也就是说,将四列数据中的每一列存储在Android端的单独字符串变量中,这样我就可以在文本视图中显示它们。

如果我不一次发送每一行的数据,那么整个表数据将连接在一个字符串中,并传递给android客户端。

任何提高效率的技巧和解决问题的线索,如果有人需要更多澄清,请询问。

我会反过来做。在将其发送到您的设备之前,我会将其格式化。使用类似JSON格式和类似的内容

package com.switchingbrains.json;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
public class JSONHelper {
    // Load JSON from URL
    public String JSONLoad(String url) {
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {
                Log.e(Main.class.toString(), "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }
}

您将得到一个字符串,您可以将其加载到JSONObject中,并用它做任何您想做的事情。

最新更新