如何在Android中解析JSON数组字符串
不幸的是,我不能说太多,只是它失败了。我试图理解如何为Android解析JSON数组。下面是我现在拥有的。我敢肯定这不是一个离工作很近的地方。我试过使用断点来查看它到底出了什么问题,但没有达到那个程度 我的代码:如何在Android中解析JSON数组字符串,android,json,Android,Json,不幸的是,我不能说太多,只是它失败了。我试图理解如何为Android解析JSON数组。下面是我现在拥有的。我敢肯定这不是一个离工作很近的地方。我试过使用断点来查看它到底出了什么问题,但没有达到那个程度 我的代码: package com.example.testers; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net
package com.example.testers;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private final String USER_AGENT = "Mozilla/5.0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONArray mJsonArray = null;
try {
mJsonArray = new JSONArray(sendGet());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject mJsonObject = new JSONObject();
for (int i = 0; i < mJsonArray.length(); i++) {
try {
mJsonObject = mJsonArray.getJSONObject(i);
mJsonObject.getString("id");
mJsonObject.getString("title");
mJsonObject.getString("post");
mJsonObject.getString("author");
mJsonObject.getString("tags");
mJsonObject.getString("datePosted");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private String sendGet() throws Exception {
String url = "http://www.craftbrothers.net/news/app.php";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
return response.toString();
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://selfsolve.apple.com/wcResults.do";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
}
}
首先,您没有对试图从JSON响应解析的值进行任何处理。当你打电话的时候
mJsonObject.getString(String)
您没有将getString结果分配给任何对象,因此您肯定不会在UI或任何日志中看到它
此外,在UI线程(也称为主线程)上调用MainActivityonCreate,因为它会阻止用户的UI冻结
通常,您应该在后台执行网络请求,然后在任务的onPostExecute方法中将结果显示给UI
根据您运行的Android版本,如果您在UI线程上发出此请求,系统可能会实际抛出一个错误,就像您一样。当我做出类似的请求时,这对我帮助很大:。把你的JSON放在那里。编辑:哦,这是有效的JSON。请在此处尝试-失败可能有2个副作用,1个崩溃2个空返回值。当你说你不能说太多的时候,你没有尽力去发布一个完整的好问题。不,这是一个合法的问题。不幸的是,我无法提供细节,因为它根本无从谈起,但我正在按照你说的做。我把它放入一个数组中。我知道我不应该把它放在那里,因为它会等到它完成请求,但是谢谢你的帮助tip@BizBink,有关AsyncTask的部分不是提示。您需要将该工作从UI线程中移除。在较新的3.0+版本的Android中,如果您尝试在UI线程上发出此请求,它实际上会失败,这可能是您甚至从未看到请求到达断点的原因。就JSON解析本身而言,您的代码看起来不错,假设您确实对结果做了一些事情。我以前在UI线程中运行过它,但不是在主线程中。除了现在在运行4.1.2而不是运行2.2时进行设备调试外,编译是相同的。我试试你说的。@BizBink,UI线程是主要线程。这两个术语是相同的。也许这会很好。