-1
我想做一個簡單的活動,它只能從數據庫獲取一個值(其中只有一個值)。 但我有一個大問題。我總是得到同樣的錯誤在logcat中:「錯誤解析data.org.json.JSONException:java.lang.String類型的值1nulln不能轉換爲JSONArray」 這裏是我的代碼:JSON和MySQL的問題
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());
}
}
}
這是PHP的文件:
<?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();
?>
我看了這麼多的線程,但我找不到答案。你有答案嗎?
謝謝你提前和對不起我的英語(我是來自德國的學生)
請發佈您的JSON。 – SASM