Android JSON异常输入结束
我已经尝试解析JSON对象很长一段时间了。这里有许多类似的问题,但是没有一个答案是有效的。我的代码没有一个是机密的,所以我在这里发布 公共类JSONParser{Android JSON异常输入结束,android,html,json,parsing,input,Android,Html,Json,Parsing,Input,我已经尝试解析JSON对象很长一段时间了。这里有许多类似的问题,但是没有一个答案是有效的。我的代码没有一个是机密的,所以我在这里发布 公共类JSONParser{ static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String
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我不确定你的Get有什么问题,但可能与我的Get相比,我一直在使用它。我会说我有输入字符0错误,这是服务器端的错误,不是我的解析错误。这意味着返回的数据可能无法在JSON对象中编码。如果我没记错的话,我想我的服务器没有返回任何东西,我基本上是在尝试解析
什么也没有
。我会验证你从服务器上得到了什么
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
}
}
}
字符串结果;
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“键”、“值”);
添加(新的BasicNameValuePair(“键2”、“值2”);
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
//要发布到的URL
HttpPost-httpreq=newhttppost(“www.sample.com/file.php”);
httpreq.setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httpreq);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
/*如果打印“结果”,则应看到有效数据
如果您的服务器正在工作*/
//系统输出打印项次(结果);
}
捕获(例外e)
{
//处理出了什么问题
}
JSONArray jArray=新JSONArray(结果);
if(jsonArray!=null)
{
for(int i=0;i
我制作了一个小测试应用程序并运行了它。HTTPPost不会返回任何内容,但如果您将其切换到HTTPGet,您会得到一个有效的响应
之后,数组中的第一个元素缺少“name”,因此当您调用c.getString(“name”)
时,会生成一个JSONException,看起来您只是在捕获外部块。您需要为每个getString
调用添加一些异常处理,例如:
String name = null;
try {
name = c.getString("name");
} catch(JSONException e) {
//name is missing!
name = "";
}
我不明白为什么要获取实体,将其转换为BufferedReader,一次读取一行实体,将其转换为字符串,将字符串转换为JSONTokener,然后最终使用标记器创建JSONObject 这里有一个更简单的方法:
String entityString = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
JSONObject json = new JSONObject(entityString);
如果抛出异常,则使用输出捕获它:
} catch (Exception e) {
e.printStackTrace();
}
并向我们显示跟踪。尝试UTF-8和/或不在
sb.append
中添加\n
。首先不要添加,JSON没有新的lines@Shehabix,这不是正确的原因。JSON可以有尽可能多的空格以使其可读。但是,在上面的代码中,不能保证readLine在prop处返回完整的行er换行符,因为源url似乎没有换行符。@323go是的,你是对的,我的意思是在内容内部,而不是在对象之间。我将解析类编辑为建议,但仍然像以前一样接收输入错误字符0的结尾。也编辑了问题中的代码。在我尝试将字符串解析为js之前,代码不会出错在对象上。我在前面放了一个字符串结果的日志,它没有打印出来,所以它确实是空的。但是已经有一个ios应用程序使用这个特定的json提要,所以我不相信问题可能是服务器端的…昨晚发现了这个问题,但不让我发布到问题。这确实是问题所在。而且这个名称在内部对象,并且必须单独进行分析。感谢您的输入!不客气。请接受我的答案为正确,或者如果您想提供更多详细信息,您可以回答自己的问题并接受正确的答案。无论哪种方式,都是接受正确答案的好形式。