Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 Http响应不完整。返回未终止的json对象_Android_Json_Http Post - Fatal编程技术网

Android Http响应不完整。返回未终止的json对象

Android Http响应不完整。返回未终止的json对象,android,json,http-post,Android,Json,Http Post,我正在使用HttpClient4.3.6执行http GET和POST请求。现在我正在使用multipartentity以文件的形式发送一些字符串参数和图像。我能够成功地发布数据,但当我得到HTTP响应时,问题就出现了。响应包含json数据 发生的情况是HTTP响应不完整,当我尝试使用数据创建json对象时,得到的jsonexception错误是: 字符407处的未终止对象 我注意到响应中不包含闭合大括号。这是安卓系统的问题还是我应该检查服务器?因为我能够在邮递员和ios上正确地看到数据。我以前

我正在使用
HttpClient
4.3.6执行http GET和POST请求。现在我正在使用multipartentity以文件的形式发送一些字符串参数和图像。我能够成功地发布数据,但当我得到HTTP响应时,问题就出现了。响应包含json数据

发生的情况是HTTP响应不完整,当我尝试使用数据创建json对象时,得到的jsonexception错误是:

字符407处的未终止对象

我注意到响应中不包含闭合大括号。这是安卓系统的问题还是我应该检查服务器?因为我能够在邮递员和ios上正确地看到数据。我以前从未面对过这个问题,也不知道如何解决这个问题

这是我发布并获得回复的代码:

@Override
protected String doInBackground(String... params) {

    try {
        String url = params[0];
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        ByteArrayBody bab = new ByteArrayBody(imageBytes, "image.jpg");
        entity.addPart("image_data", bab);
        entity.addPart("action", new StringBody("1", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("name", new StringBody("asdfg", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("user_id", new StringBody("157", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("birthday", new StringBody("18-04-1995", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("gender", new StringBody("male", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("is_jlpt_student", new StringBody(String.valueOf(0), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("relationship", new StringBody("Father", "text/plain", Charset.forName("UTF-8")));
        entity.addPart("relationship_id", new StringBody(String.valueOf(10002), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("is_creator", new StringBody(String.valueOf(1), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("email", new StringBody(email, "text/plain", Charset.forName("UTF-8")));

        httppost.setEntity(entity);
        HttpResponse resp = httpclient.execute(httppost);
        String response = EntityUtils.toString(resp.getEntity());
        Log.i("HttpResponse", response);

        return response;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}

@Override
protected void onPostExecute (String result) {
    super.onPostExecute(result);

    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(result);
        JSONObject json_data = jsonObject.getJSONObject("data");
        String json_userid = json_data.getString("user_id");
        String json_username = json_data.getString("name");
        String json_email = json_data.getString("email");
        String json_country = json_data.getString("country_code");
        String json_imagefilename = json_data.getString("image_filename");
        String json_imgurl = json_data.getString("image_url");

        Toast.makeText(ParentGuardianProfile.this, "ImageFile " + json_imagefilename, Toast.LENGTH_SHORT).show();


       User new_user = userdao.createUser(json_userid, json_username, json_email,json_imagefilename,json_country,selectedImageUri.toString(), 1);


       Log.i("SQLITE", "added user : " + new_user.getmUserName() + new_user.getmId());


    } catch (JSONException e) {
        e.printStackTrace();
    }
}                             
我的json响应是:

{"status":1,"message":"success","data":{"child_id":"381","name":"asdfg","image_filename":"C201603021734476.jpg","image_url":"https:\/\/innokid.blob.core.windows.net\/media\/child\/381.jpg","birthday":"18-04-1995","gender":"male","is_jltp_student":"0","relationship":"Father","relationship_id":"10002","is_creator":1,"rank":1,"qrcode_url":"http:\/\/innokid.azurewebsites.net\/uploads\/qrcode\/child_381.png"

我试着按照本文的建议使用字符串缓冲区。但我还是得到了同样的结果

代码乍一看是正常的。 您是如何知道json数据被剪切的?Logcat可以截断文本。在这种情况下,调试器应该更可靠

尝试使用一些工具(如curl/SoapUI)生成相同的请求,并使用一些格式化程序/验证器验证JSON(您很容易找到一些这样的工具)


这超出了问题的范围,但使用原始的Android内置通信库似乎有点自虐。你曾经考虑过使用改装吗?

< P>代码乍一看看起来不错。 您是如何知道json数据被剪切的?Logcat可以截断文本。在这种情况下,调试器应该更可靠

尝试使用一些工具(如curl/SoapUI)生成相同的请求,并使用一些格式化程序/验证器验证JSON(您很容易找到一些这样的工具)


这超出了问题的范围,但使用原始的Android内置通信库似乎有点自虐。你曾经考虑过使用改型吗?

< P>我认为这个代码是有问题的:代码> String Retry= EntIUTILLS.ToString(RES.GETEnTyTE());代码>

可能您应该使用其他函数将响应转换为字符串


显然,json结尾缺少两个大括号“}”,这可能是由于toString代码中的一些错误造成的。

我认为这段代码有问题
String response=EntityUtils.toString(resp.getEntity())

可能您应该使用其他函数将响应转换为字符串


显然,json在结尾缺少了两个大括号“}”,这可能是由于toString代码中的一些错误造成的。

我调出了一个使用
org.apache.http
的旧项目,下面是我如何解析响应的。正如你所看到的,它相当麻烦。有许多经过测试和维护的库更适合这种重载

//获取响应实体(->数据):
HttpEntity=response.getEntity();
如果(实体!=null){
//阅读内容流
InputStream instream=entity.getContent();
Header contentEncoding=response.getFirstHeader(“内容编码”);
if(contentEncoding!=null&&contentEncoding.getValue().equalsIgnoreCase(“gzip”)){
流入=新的GZIPInputStream(流入);
}
//将内容流转换为字符串
结果字符串=转换流到字符串(流内);
流内关闭();
//在这里使用resultString进行操作
//消费内容
entity.consumercontent();
}
以及
convertStreamToString()
方法:

private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the
         * BufferedReader.readLine() method. We iterate until the BufferedReader
         * return null which means there's no more data to read. Each line will
         * appended to a StringBuilder and returned as String.
         * 
         * (c) public domain:
         * http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/
         * 11/a-simple-restful-client-at-android/
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

我调出了一个使用
org.apache.http
的老项目,下面是我如何解析响应的。正如你所看到的,它相当麻烦。有许多经过测试和维护的库更适合这种重载

//获取响应实体(->数据):
HttpEntity=response.getEntity();
如果(实体!=null){
//阅读内容流
InputStream instream=entity.getContent();
Header contentEncoding=response.getFirstHeader(“内容编码”);
if(contentEncoding!=null&&contentEncoding.getValue().equalsIgnoreCase(“gzip”)){
流入=新的GZIPInputStream(流入);
}
//将内容流转换为字符串
结果字符串=转换流到字符串(流内);
流内关闭();
//在这里使用resultString进行操作
//消费内容
entity.consumercontent();
}
以及
convertStreamToString()
方法:

private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the
         * BufferedReader.readLine() method. We iterate until the BufferedReader
         * return null which means there's no more data to read. Each line will
         * appended to a StringBuilder and returned as String.
         * 
         * (c) public domain:
         * http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/
         * 11/a-simple-restful-client-at-android/
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

我最终解决了这个问题,用Android异步Http客户端替换了httpclient库。现在它工作得很好。非常感谢你的帮助!
但是,我仍然不明白为什么在使用httpclient时响应被截断。

我最终用Android异步Http客户端替换httpclient库解决了这个问题。现在它工作得很好。非常感谢你的帮助!
但是,我仍然不理解为什么在使用httpclient时响应被截断。

这是您的完整json响应吗?请检查服务器代码,或者它的编码是否正确,或者它是否是您的完整json响应?请检查服务器代码,或者它的编码是否正确。我在调试器中检查了响应字符串。它没有JSONObject的右大括号,因此我的建议是使用我在Postman上检查过的一些独立工具检查服务器返回的内容,并且响应正确显示。在调试器中,我检查了响应字符串,但未检查任何字符。它没有JSONObject的右括号,所以我的建议是使用我在Postman上检查过的一些独立工具检查服务器返回的内容,并且响应正确显示。没有遗漏任何字符