0

我在java(android)中有以下代码:

            String image = null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("pic_image",image));
    TextView tv1 = (TextView) findViewById(R.id.image); 
    DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new    HttpPost("http://www.carprotectionsystem.webuda.com/image.php");         

    try {   
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response  = httpclient.execute(httppost);
        if(response.getStatusLine().getStatusCode()==200)
        {
        HttpEntity entity = response.getEntity();
        if(entity !=null)
        {
        InputStream instream=entity.getContent();
        JSONObject jsonResponse = null;
        try {
            jsonResponse = new JSONObject(convertStreamToString(instream));
        } catch (Exception e) {
        }
        try {
            image =jsonResponse.getString("pic_image");
        } catch (Exception e) {
        } 
        tv1.setText(image);
        }
                     }   
              } 
            catch (Exception e) 
                    {
    Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_SHORT).show();
       }    }

并且 php 脚本在浏览器中给了我这个响应:{"pic_image":"a"}

我想在 android 的文本视图中显示“a”,但出现此错误:
org.json.JSONexception : value DOCTYPE of type java.lang.string cannot be converted to JSONObject
任何帮助?

4

2 回答 2

0

查看服务器的响应(在浏览器中,右键单击 -> 显示源)。您将看到它向您发送 html 内容而不是 JSON 内容。这是您收到的内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>{"pic_image":"a"}</body>
</html>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

您不能将其解析为 JSONObject。

于 2013-09-20T09:27:46.923 回答
-1
Use this method to get string from response entity:

    public String convertStreamToString(final InputStream instream) throws IOException, ParseException {

        if (instream == null) {
            return "";
        }

        StringBuilder buffer = new StringBuilder();

        BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "utf-8"));

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

        } finally {
            instream.close();
            reader.close();
        }
        System.out.println(buffer.toString());
        return buffer.toString();

    }
于 2013-09-20T09:35:49.230 回答