我一直在尝试解析JSON对象很长一段时间。这里有许多类似的问题,但没有一个有答案可行。我的代码都没有保密,所以我在这里发帖。
公共类JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
//HttpPost httpPost = new HttpPost(url);
HttpGet httpget = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
JSONTokener tokener = new JSONTokener(json);
jObj = new JSONObject(tokener);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
*编辑Get是必要的而不是Post
答案 0 :(得分:0)
不确定你的错误,但也许与我一直在使用的那个相比。我会说我有输入字符0错误,这是一个 SERVER SIDE 错误,而不是我的解析。这意味着返回的数据可能无法在JSON对象中进行编码。如果我没记错的话,我认为我的服务器什么都没有返回,我基本上是在尝试解析nothing
。我会验证您从服务器获得的内容。
String result;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("key", "value");
nameValuePairs.add(new BasicNameValuePair("key2", "value2");
try
{
HttpClient httpclient = new DefaultHttpClient();
// URL to POST to
HttpPost httpreq = new HttpPost("www.sample.com/file.php");
httpreq.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpreq);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
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();
/* if you print 'result' you should see valid data
if your server is working */
// System.out.println(result);
}
catch (Exception e)
{
// handle what went wrong
}
JSONArray jArray = new JSONArray(result);
if (jsonArray != null)
{
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject json_data;
try
{
json_data = jsonArray.getJSONObject(i);
String value = json_data.getString("json_key_here");
}
catch (JSONException e)
{
// handle what went wrong
}
}
}
答案 1 :(得分:0)
我做了一个小测试应用程序并运行它。 HTTPPost什么都不返回,但是如果你把它切换到HTTPGet,你会得到一个有效的响应。
之后,数组中的第一个元素缺少“name”,因此当您调用c.getString("name")
时,会生成一个JSONException,看起来您只是在捕获外部块。您需要为每个getString
调用添加一些异常处理,可能类似于:
String name = null;
try {
name = c.getString("name");
} catch(JSONException e) {
//name is missing!
name = "";
}
答案 2 :(得分:0)
我不明白你为什么拿这个实体,将它转换为BufferedReader,一次读取一行实体,将其转换为String,将String转换为JSONTokener,最后使用tokener创建一个JSONObject。
这是一种更简单的方法:
String entityString = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
JSONObject json = new JSONObject(entityString);
如果抛出异常,则用输出:
捕获它} catch (Exception e) {
e.printStackTrace();
}
向我们展示痕迹。