Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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
获取org.json.JSONException的原因:java.lang.String类型的Value属性无法转换为JSONObject?_Java_Android_Json_String - Fatal编程技术网

获取org.json.JSONException的原因:java.lang.String类型的Value属性无法转换为JSONObject?

获取org.json.JSONException的原因:java.lang.String类型的Value属性无法转换为JSONObject?,java,android,json,string,Java,Android,Json,String,通过使用它,我得到了响应代码,并试图将字符串转换为JSONObject,但得到了异常 public JSONObject getJSONFromUrl(String url,List<NameValuePair> nameValuePairsList) { // Making HTTP request try { HttpParams param = new BasicHttpParams(); Http

通过使用它,我得到了响应代码,并试图将字符串转换为JSONObject,但得到了异常

public JSONObject getJSONFromUrl(String url,List<NameValuePair> nameValuePairsList) {
        // Making HTTP request
        try {
            HttpParams param = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(param, 20000);
            HttpConnectionParams.setSoTimeout(param, 20000);
            DefaultHttpClient httpClient = new DefaultHttpClient(param);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairsList));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            PropertyLogger.debug("URL Request: ", url.toString());
            PropertyLogger.debug("Encoded Params: ", nameValuePairsList.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            int code = httpResponse.getStatusLine().getStatusCode();
            if (code != 200) {
                PropertyLogger.debug("HTTP response code is:", Integer.toString(code));
                return null;
            } else {
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
        } catch (ConnectTimeoutException e) {
            // TODO: handle exception
            PropertyLogger.error("Timeout Exception", e.toString());
            return null;
        } catch (SocketTimeoutException e) {
            // TODO: handle exception
            PropertyLogger.error("Socket Time out", e.toString());
            return null;
        } catch (UnsupportedEncodingException e) {
            PropertyLogger.error("UnSupported Exception", e.toString());
            e.printStackTrace();
            return null;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String TAG = "PropertyJsonParser";
        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();
            jsonResp = sb.toString();
            PropertyLogger.debug("Content: ", sb.toString());
        } catch (Exception e) {
            PropertyLogger.error("Buffer Error", "Error converting Response " + e.toString());
            return null;
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(jsonResp);
        } catch (JSONException e) {
            PropertyLogger.error("JSON Parser", "Error parsing data :" + e.toString());
            return null;
        }

        return jObj;
    }
publicJSONObject getJSONFromUrl(字符串url,列表名ValuePairsList){
//发出HTTP请求
试一试{
HttpParams param=新的BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(参数,20000);
HttpConnectionParams.setSoTimeout(参数,20000);
DefaultHttpClient httpClient=新的DefaultHttpClient(参数);
HttpPost HttpPost=新的HttpPost(url);
setHeader(HTTP.CONTENT_TYPE,“application/x-www-form-urlencoded”);
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairsList));
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
调试(“URL请求:,URL.toString());
调试(“编码参数:”,nameValuePairsList.toString());
HttpResponse HttpResponse=httpClient.execute(httpPost);
int code=httpResponse.getStatusLine().getStatusCode();
如果(代码!=200){
调试(“HTTP响应代码为:”,Integer.toString(代码));
返回null;
}否则{
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(ConnectTimeoutException e){
//TODO:处理异常
错误(“超时异常”,例如toString());
返回null;
}捕获(SocketTimeoutException e){
//TODO:处理异常
错误(“套接字超时”,例如toString());
返回null;
}捕获(不支持的编码异常e){
错误(“不支持的异常”,例如toString());
e、 printStackTrace();
返回null;
}捕获(客户端协议例外e){
e、 printStackTrace();
返回null;
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
String TAG=“PropertyJsonParser”;
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
jsonResp=sb.toString();
调试(“内容:,sb.toString());
}捕获(例外e){
PropertyLogger.error(“缓冲区错误”,“错误转换响应”+e.toString());
返回null;
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(jsonResp);
}捕获(JSONException e){
错误(“JSON解析器”,“错误解析数据:”+e.toString());
返回null;
}
返回jObj;
}

因为变量jsonResp包含字符串值而不是JSONObject,所以请首先打印并确保它是否为字符串类型值。

由于没有收到正确的JSON响应,因此出现此错误


检查您的api响应,并可能提供您的api响应。

记录jsonResp并告诉我们它是什么。在转换为json对象并检查它是否为json格式之前,您没有获得有效的jsonlog您的jsonResp吗?
jsonResp
如何不是一个
字符串
?因为您的错误仅表明此错误将字符串值放入JSONObject时发生。