2014-03-03 76 views
0

我想從mysql數據庫獲取一些數據。
PHP代碼:錯誤轉換(JSON)結果

<?php 
$verbindung = mysql_connect ("", 
"", "") 
or die ("keine Verbindung möglich. 
Benutzername oder Passwort sind falsch"); 

mysql_select_db("DB1539594") 
or die ("Die Datenbank existiert nicht."); 

$q=mysql_query("SELECT * FROM status WHERE ID>'".$_REQUEST['ID']."'"); 
while($e=mysql_fetch_assoc($q)) 
     $output[]=$e; 

print(json_encode($output)); 

mysql_close(); 
?> 

如果執行它看起來像這樣的文件:

[{"ID":"1","name":"tester","status":"1","reports":"5","lastTime":"2014-02-27 17:48:25"}] 

我最大的問題是,我得到一個FATAL EXCEPTION: AsyncTask #1

這裏是我的代碼:

public class PublicCheck extends Activity { 
    String URL2; 
    String result; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_public_check); 
     URL2 = "http://domain.com/real.php"; 
    } 


    public void check() { 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     String x; 

      task.execute(URL2); 


     //Log.i("Ergebnis", x+ URL2); 

    } 

    class DownloadWebPageTask extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... urls) { 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(
         urls[0]); 
       try { 

        HttpResponse response = httpclient.execute(httppost); 
         result = inputStreamToString(
            response.getEntity().getContent()).toString(); 
           } 

           catch (ClientProtocolException e) { 
           e.printStackTrace(); 
           } catch (IOException e) { 
           e.printStackTrace(); 
           } 

      return null; 
     } 

       private StringBuilder inputStreamToString(InputStream is) { 
         String rLine = ""; 
         StringBuilder answer = new StringBuilder(); 
         BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 

         try { 
         while ((rLine = rd.readLine()) != null) { 
         answer.append(rLine); 
         } 
         } 

         catch (IOException e) { 
         // e.printStackTrace(); 
         Toast.makeText(getApplicationContext(), 
          "Error..." + e.toString(), Toast.LENGTH_LONG).show(); 
         } 
         return answer; 
         } 




     @Override 
     protected void onPostExecute(String result) { 
      try{ 
       JSONArray jArray = new JSONArray(result); 
       for(int i=0;i<jArray.length();i++){ 
         JSONObject json_data = jArray.getJSONObject(i); 
         Log.i("log_tag","id: "+json_data.getInt("ID")+ 
           ", name: "+json_data.getString("name")+ 
           ", status: "+json_data.getInt("status")+ 
           ", reports: "+json_data.getInt("reports") 
         ); 
       } 
     } 
     catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 
     } 
    } 
} 

我不知道我能做什麼。如果你能幫助我,我會很高興:)

這些是錯誤。

enter image description here

+0

您是否嘗試過檢查服務器的響應? – Nitish

+0

什麼是'urls [0]'...? – SilentKiller

+0

@SilentKiller它是OP從中獲取數據的URL – Nitish

回答

0

檢查你的HTTP調用從調試器返回的數據正確的數據或不。如果是,那麼請使用以下代碼讀取字符串。

HttpResponse response = httpclient.execute(httpget); 
    EntityUtils.toString(response.getEntity()); 

不要呼籲異步任務此get方法

x = task.execute(URL2).get(); 

更換

x = task.execute(URL2); 

試試這個從URL

try { 
       HttpPost httppost = new HttpPost(
         urls[0]); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response1 = httpclient.execute(httppost); 
       if(response1 != null) 
     { 
         HttpEntity httpEntity = response1.getEntity(); 
         result = EntityUtils.toString(httpEntity); 
       } 
    } 
    catch (Exception e) 
    { 
       Log.e("log_tag", "Error converting result " + e.toString()); 

      } 
+0

對不起,我不知道該怎麼辦。你能解釋一下嗎? –

+0

該死的,我得到了同樣的錯誤。我想我正在尋找另一種解決方案... –

+0

現在看來它不是這樣... public void check(){ \t \t DownloadWebPageTask task = new DownloadWebPageTask(); \t \t AsyncTask x; \t \t x = task.execute(URL2); \t \t \t \t // Log。我(「Ergebnis」,x + URL2); \t} –

2

讀取數據試試這個..

你給全局變量String URL2;然後

在你onCreate再次你給String URL2 = "http://domain.com/real.php";

裏面check你叫DownloadWebPageTaskAsyncTaskx = task.execute(URL2).get();URL2爲null,則它會給NPE

只需刪除String裏面onCreate就像URL2 = "http://domain.com/real.php";

+0

感謝您的回答,但沒有任何變化:( –