Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Logcat上的JSON错误。无法从Logcat中找到错误_Android_Json - Fatal编程技术网

Android Logcat上的JSON错误。无法从Logcat中找到错误

Android Logcat上的JSON错误。无法从Logcat中找到错误,android,json,Android,Json,我正在尝试在我的应用程序中解析JSON 我的JSONParser.java如下所示 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public JSONObject getJSONFromUrl(String url, List<NameValuePair> params

我正在尝试在我的应用程序中解析JSON

我的JSONParser.java如下所示

public class JSONParser { 
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";


    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        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, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "n");
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
公共JSONObject getJSONFromUrl(字符串url,列表参数){
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.附加(第+行“n”);
}
is.close();
json=sb.toString();
Log.e(“JSON”,JSON);
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
}

我已经从这里将JSON发送到设备

这是pastebin的输出

这在我的日志里。

有人能解释一下问题出在哪里吗

sb.append(line + "n");
也许你是想写信

sb.append(line + "\n");
EntityUtils还有静态方法
toString(entity))
。您可以直接获取您的
字符串
这样

HttpEntity httpEntity = httpResponse.getEntity();
String jsonString = EntityUtils.toString(httpEntity);
然后

try {
        jObj = new JSONObject(jsonString);
 } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
 }
也许你是想写信

sb.append(line + "\n");
EntityUtils还有静态方法
toString(entity))
。您可以直接获取您的
字符串
这样

HttpEntity httpEntity = httpResponse.getEntity();
String jsonString = EntityUtils.toString(httpEntity);
然后

try {
        jObj = new JSONObject(jsonString);
 } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
 }

getJSONFromUrl是否在Ui线程上运行?您的logcat正常。但是您应该删除
Log.e(“JSON”,JSON)。为什么在结果成功后要记录错误?printStackTrace而不是log e.toString.you log是这一行
log.e(“JSON”,JSON)人们可以停止使用iso8859-1读取json吗?json是utf-8。如果你继续这样做,你会有麻烦的。此外,还有更高效的EntityUtils,可以在一行中读取HttpEntity的内容。我不明白人们为什么使用readLine。行是一个任意的分隔符,不需要每个人都具有相同的大小。getJSONFromUrl是否在Ui线程上运行?您的logcat正常。但是您应该删除
Log.e(“JSON”,JSON)。为什么在结果成功后要记录错误?printStackTrace而不是log e.toString.you log是这一行
log.e(“JSON”,JSON)人们可以停止使用iso8859-1读取json吗?json是utf-8。如果你继续这样做,你会有麻烦的。此外,还有更高效的EntityUtils,可以在一行中读取HttpEntity的内容。我不明白人们为什么使用readLine。行是一个任意的分隔符,不需要每个人都有相同的大小。嘿,谢谢。我是一个初学者,所以不知道所有的细节。我将把它附加到我的代码中。还感谢您捕获该错误。我完全没有意识到这一点。你对代码还有什么建议吗?使用EntityUtils istead of BufferRead,这次:)嘿,谢谢。我是一个初学者,所以不知道所有的细节。我将把它附加到我的代码中。还感谢您捕获该错误。我完全没有意识到这一点。您对代码还有什么建议吗。使用EntityUtils istead of BufferRead,这次:)