错误解析JSON数据(java.lang.String类型的值不能转换为JSON数组) [英] error parsing json data (value of type java.lang.String cannot be converted to json array)

查看:176
本文介绍了错误解析JSON数据(java.lang.String类型的值不能转换为JSON数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我在哪里试图检索在PHP站点数据库的价值,简单的Andr​​oid应用程序,但下面code生成错误解析JSON数据。

i am trying to implement simple android application where i am trying to retrieve the database values over php site, but the following code generates error parsing json data.

我的PHP文件编码JSON是如下

my php file encoding json is as below

getdeals.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="user"; // Database name
$tbl_name="deal"; // Table name

// Connect to server and select databse.
mysql_connect("$host","$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$q=mysql_query("SELECT * FROM deal");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output));

mysql_close();
?>

表交易是如下

table deal is as below

安卓类是如下

public class JsonConnect extends Activity {

    InputStream is;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json_connect);
        addListenerOnbutton();
        Thread t = new Thread() {
            public void run() {
                getDeals();
            }
        };
        t.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_json_connect, menu);
        return true;
    }

    public void addListenerOnbutton() {

        final Context context = this;

        Button home = (Button)findViewById(R.id.button1);

        home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();  

            }

        });
 }

    public void getDeals() {

        String result = "";
        //the year data to send
        //ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //nameValuePairs.add(new BasicNameValuePair("year","1980"));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/dealnow/getdeals.php");
                //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        }
        catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
        //convert response to string
        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();

                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        //parse json data
        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","dealid: "+json_data.getString("deal_id")+
                                ", hotel name: "+json_data.getString("hotel_name")+
                                ", valid date: "+json_data.getInt("valid_date")+
                                ", location: "+json_data.getString("location")+
                                ", website: "+json_data.getString("website")
                        );
                }
        }
        catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }
}

和最后的logcat输出

and finally logcat output is

05-11 01:23:08.767: D/dalvikvm(435): GC_EXPLICIT freed 24K, 4% free 6582K/6855K, paused 7ms+4ms

05-11 01:29:24.164: V/TLINE(497): new: android.text.TextLine@4065aa30

05-11 01:29:24.757: D/dalvikvm(497): GC_CONCURRENT freed 115K, 4% free 6560K/6791K, paused 5ms+14ms

05-11 01:29:24.764: E/log_tag(497): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray

05-11 01:29:24.844: V/TLINE(497): new: android.text.TextLine@4065d8e0

05-11 01:34:12.147: D/dalvikvm(497): GC_EXPLICIT freed 48K, 4% free 6524K/6791K, paused 7ms+3ms

我怎样才能获得该表的所需值

how can i get the desired values of the table

请帮忙。

推荐答案

由PHP脚本返回的响应不是有效的JSON格式。

The response returned by PHP script is not in valid JSON format.

您需要把 $输出=阵列(); while循环在什么地方

You need to put $output=array(); somewhere before the while loop

这篇关于错误解析JSON数据(java.lang.String类型的值不能转换为JSON数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆