Java JSON和MySQL的问题

Java JSON和MySQL的问题,java,php,android,mysql,json,Java,Php,Android,Mysql,Json,我想做一个简单的活动,它只从数据库中获取一个值(其中只有1个值)。 但我有一个大问题。我总是在LogCat中遇到同样的错误:“解析data.org.json.JSONException时出错:java.lang.String类型的值1nulln无法转换为JSONArray。” 这是我的密码: public class DailyQ extends Activity { InputStream is; String quest; JSONObject json_data;

我想做一个简单的活动,它只从数据库中获取一个值(其中只有1个值)。 但我有一个大问题。我总是在LogCat中遇到同样的错误:“解析data.org.json.JSONException时出错:java.lang.String类型的值1nulln无法转换为JSONArray。” 这是我的密码:

public class DailyQ extends Activity {
    InputStream is;
    String quest;
    JSONObject json_data;
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.daily_q);
            getData();

     }


            public void getData() {
                StrictMode.ThreadPolicy policy = new
                        StrictMode.ThreadPolicy.Builder()
                        .permitAll().build(); 
                        StrictMode.setThreadPolicy(policy);

                String result = "";
                     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                     try{
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://domain.com/file.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", "Fehler bei der http Verbindung "+e.toString());
                     }
                     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());

                             }
                     try{
                             JSONArray jArray = new JSONArray(result);
                             for(int i=0;i<jArray.length();i++){
                             json_data = jArray.getJSONObject(i);
                             int id = json_data.getInt("day");
                             quest = json_data.getString("question");                        
                             TextView tv = (TextView) findViewById(R.id.textView1);
                           tv.setText(quest);
                             } 

                             }
                             catch(JSONException e){
                          Log.e("log_tag", "Error parsing data "+e.toString());
                             }
                            }
            }
公共类DailyQ扩展活动{
输入流为;
弦乐探索;
JSONObject json_数据;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.daily_q);
getData();
}
public void getData(){
StrictMode.ThreadPolicy策略=新建
StrictMode.ThreadPolicy.Builder()文件
.permitAll().build();
StrictMode.setThreadPolicy(策略);
字符串结果=”;
ArrayList nameValuePairs=新的ArrayList();
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://domain.com/file.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
Log.e(“Log_标记”,“Fehler bei der http Verbindung”+e.toString());
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.附加(第+行“n”);
} 
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
试一试{
JSONArray jArray=新JSONArray(结果);

对于(int i=0;i我不相信您的
结果是json编码的字符串,因此您的JSONArray函数失败:

查看

JSON字符串应类似于:



您可能只需将
{
附加到
结果的前面和
}
后面,就可以了。

请发布您的JSON。
<?php
mysql_connect(...) or die ("Keine Verbindung");
mysql_select_db("...);

$q=mysql_query("SELECT day,question FROM quest");
while($e=mysql_fetch_assoc($q))
$output[]=$e;

print(json_encode($output));
 mysql_close();
?>
{"1": "1_data", "2": "2_data", "3": "3_data"}