dongliang1893 2014-07-29 13:28
浏览 46

无法连接到db android,因为解析json时出错

I can't connect and display my json from my website because i get an error:

java.lang.String cannot be converted to JSONArray

this is the code:

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    private TextView results;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        results = (TextView) findViewById(R.id.results);
        results.setText(invioDati());
    }

    public String invioDati() {
        String risultati = "";
        String stringaFinale = "";

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("idnomerichiesto","1"));
        InputStream is = null;

        //HTTP post richiesta

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.davidedellai.net/jsonphp.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Toast.makeText(this, "Errore connessione", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
        if (is != null) {
            // converto la risposta in stringa
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "
");
                }
                is.close();
                risultati = sb.toString();
            }catch (Exception e) {
                // errore conversione in stringa
                Toast.makeText(this, "Errore stringa "+e.toString(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

            // parsing dei dati dal json
            try {
                JSONArray jArray = new JSONArray(risultati);
                for (int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("Test", "post_title " + json_data.getString("post_title"));
                    stringaFinale = json_data.getString("post_title") + " " + (json_data.getString("post_content"));

                }
            } catch(JSONException e) {
                //errore parsing data json
                Toast.makeText(this, "Errore parsing json", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        } else {
            // null quindi nessuna risposta
            Toast.makeText(this, "Null", Toast.LENGTH_SHORT).show();

        }

        return stringaFinale;
    }
}

the php part in my website is:

<?php 
// parametri del database 
$db_host = "localhost"; 
$db_user = "user"; 
$db_password = ""; 
$db_name = "mydb"; 
$db = mysql_connect($db_host, $db_user, $db_password); 
if ($db == FALSE) 
die ("Errore nella connessione. Verificare i parametri..."); 
mysql_select_db($db_name, $db) 
or die ("Errore nella selezione del database. Verificare i parametri..."); 
$q=mysql_query("SELECT post_name, post_content FROM avwp_posts WHERE post_status = 'publish' "); 
while($e=mysql_fetch_assoc($q)) 
$output[]=$e; 

print json_encode(array($output)); 

mysql_close();

?>

I can't display any result.. and it returns the error in JSONArray at line JSONArray jArray = new JSONArray(risultati); . Is the php the problem or java? The connection to db seems correct..

log: enter image description here

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?