gpt4 book ai didi

java - 在android中使用JSON解析数据

转载 作者:行者123 更新时间:2023-12-02 06:59:36 26 4
gpt4 key购买 nike

我正在使用 json 对象解析数据,并且数据也是从服务器解析的,它显示在控制台中,但在模拟器中崩溃了..

这是我的代码:

public class jsonUtil {
static HttpURLConnection urlConnection;

public static HttpURLConnection openConnection() throws Exception {
int responseCode = -1;
try {
URL url = new URL("url display");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-encoded");
urlConnection.setRequestProperty(
"Content-Length",
"" + Integer.toString("category=breaking_news&latitude=&longitude="
.getBytes().length));
urlConnection.setRequestProperty("Content-Language", "en-US");
urlConnection.setRequestProperty("Accept-Encoding", "identity");
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setReadTimeout(10 * 1000);
urlConnection.setConnectTimeout(10 * 1000);

DataOutputStream wr = new DataOutputStream(
urlConnection.getOutputStream());
wr.writeBytes("category=breaking_news&latitude=&longitude=");
wr.flush();
wr.close();
responseCode = urlConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("Server not responding");
}
} catch (SocketException e) {
throw new Exception("Connection Time Out");
} catch (java.net.UnknownHostException unknownHostException) {
// TODO: handle exception
throw new Exception("unknownHostException");
} catch (Exception e) {
// TODO: handle exception
throw new Exception("Error Occured");
}
return urlConnection;
}

public static ArrayList<String> jsonParseList = new ArrayList<String>();

public static ArrayList<String> jsonParsing() {
StringBuffer buffer = new StringBuffer();
JSONArray array;

try {
try {
urlConnection = openConnection();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line = bufferedReader.readLine();
while (line != null) {
buffer.append(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
if (buffer.toString() != null) {
try {
array = new JSONArray(buffer.toString());
JSONObject jsonObject;
for (int i = 0; i < array.length(); i++) {
jsonObject = array.getJSONObject(i);
String conv_title = jsonObject.getString("conv_title");
String content = jsonObject.getJSONObject("first_post")
.getString("content");
String creator = jsonObject.getJSONObject("first_post")
.getJSONObject("creator").getString("name");
Log.e("List Values", conv_title);
jsonParseList.add(conv_title);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonParseList;
}
}

日志猫:

' 06:35:21.637: E/List Activity (795): []05-30 06:35:21.687:W/Trace(795):来自nativeGetEnabledTags的意外值:005-30 06:35:21.697:W/Trace(795):来自nativeGetEnabledTags的意外值:005-30 06:35:21.717:W/Trace(795):来自nativeGetEnabledTags的意外值:005-30 06:35:21.927:我/Choreographer(795):跳过了 144 帧! (795):nativeGetEnabledTags 出现意外值:005-30 06:35:22.158:我/编舞(795):跳过了 48 帧!应用程序可能在其主线程上做了太多工作。05-30 06:35:22.317:W/Trace(795):来自nativeGetEnabledTags的意外值:005-30 06:35:22.317:W/Trace(795):来自nativeGetEnabledTags的意外值:005-30 06:35:22.337:W/Trace(795):来自nativeGetEnabledTags的意外值:005-30 06:35:23.485:D/dalvikvm(795):GC_CONCURRENT释放276K,12%释放2894K/3288K,暂停69ms + 3ms,总共134ms05-30 06:35:23.642:E/List Values(795):总理曼莫汉·辛格今天将提交 UPA II 成绩单:E/List Values(795):美国、印度 promise 合作打击恐怖主义05-30 06:35:23.652:E/List Values(795):美国和印度 promise 合作打击恐怖主义05-30 06:35:23.652:E/List Values(795):巴基斯坦的真实动机暴露05-30 06:35:23.652:E/List Values(795):巴基斯坦的真实动机暴露05-30 06:35:23.657:E/List Values(795):卢比小幅回升,早盘上涨 6 派萨05-30 06:35:23.657:E/List Values(795):卢比小幅回升,早盘上涨 6 派萨05-30 06:35:23.657:E/List 值(795):UP 的 43 名 IPS 官员在其职业生涯中调动了 40 次05-30 06:35:23.657:E/List 值(795):UP 的 43 名 IPS 官员在其职业生涯中调动了 40 次05-30 06:35:23.657:E/List Values(795):Allahabad HC 拒绝 Talwars 询问 14 名证人的请求05-30 06:35:23.657:E/List Values(795):国防部长 Shashi Kant Sharma 将接任新任 CAG05-30 CAG05-30 06:35:23.691:E/List 值(795):俄克拉荷马州 Tornado 的威力使广岛原子弹相形见绌05-30 06:35:23.691:E/List Values(795):银行因不偿还定期存款而被罚款 50,000 卢比05-30 06:35:58.607:W/Trace(795):nativeGetEnabledTags 出现意外值:0

最佳答案

我不喜欢通过提供代码来给你喂食,所以请检查下面的链接Android JSON 解析教程可在下面的链接中找到,您的问题将使用此解决 http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

关于java - 在android中使用JSON解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16829373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com