2012-11-17 62 views
0

我努力完成我的項目JSON,PHP和MySQL

這是我的計劃

import java.util.ArrayList; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 




import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Search extends Activity { 

    TextView error,br2; 
    EditText namjal2; 
    String i,returnString2; 


    /** Called when the activity is first created. */ 

    @Override 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.search); 

     br2=(TextView)findViewById(R.id.berita2); 
     namjal2=(EditText)findViewById(R.id.namjalSearch); 




    } 

    public void clickHandler(View view){ 
    Intent a = null; 
    switch (view.getId()){ 
    case R.id.find: 

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 

postParameters.add(new BasicNameValuePair("nama_jalan", namjal2.getText().toString())); 


/*   String valid = "1";*/  

     String response = null; 

     try { 

      response = CustomHttpClient.executeHttpPost("http://10.0.2.2/android/searchInfo.php", postParameters); 


      String result = response.toString(); 
      //parse json data 

       try{ 
         returnString2 = ""; 


       JSONArray jArray = new JSONArray(result); 
         for(int i=0;i<jArray.length();i++){ 

           JSONObject json_data = jArray.getJSONObject(i); 
           Log.i("log_tag","id_kepadatan: "+json_data.getInt("id_kepadatan")+ 
             ", username: "+json_data.getString("username")+ 
             ", nama_jalan: "+json_data.getString("nama_jalan")+ 
             ", status: "+json_data.getString("status")+ 
             ", tanggal: "+json_data.getString("tanggal")+ 
             ", waktu: "+json_data.getString("waktu")+ 
             ", keterangan: "+json_data.getString("keterangan") 
          ); 

           //Get an output to the screen 
           returnString2 += "\n" + json_data.getString("nama_jalan") + " mengalami "+ json_data.getString("status")+ " pada "+ json_data.getString("tanggal")+ " waktu "+ json_data.getString("waktu")+ " karena "+ json_data.getString("keterangan"); 
         } 
       } 


       catch(JSONException e){ 
         Log.e("log_tag", "Error parsing data "+e.toString()); 
       } 

       try{ 


       br2.setText(returnString2); 


       } 


       catch(Exception e){ 

       Log.e("log_tag","Error in Display!" + e.toString());;   

       } 

     } 

       catch (Exception e) { 

      Log.e("log_tag","Error in http connection!!" + e.toString());  

     } 
     break; 

    case R.id.back: 
     a = new Intent(this, Menu.class); 
     startActivity(a); 
     break; 
} 
    } 


} 

,這我searchInfo.php

<?php 
    date_default_timezone_set('asia/jakarta'); 
    $skrg = time(); 
    $tgl = date("dmY",$skrg); 

$conn = mysql_connect("localhost","root",""); 
    mysql_select_db("proyek akhir"); 

$query = "SELECT * FROM kepadatan WHERE nama_jalan = '". $_POST["nama_jalan"]."' AND tanggal ='$tgl'"; 
$result = mysql_query($query); 
while($row=mysql_fetch_assoc($result)) 
$output[]=$row; 
print(json_encode($output)); 
mysql_close(); 
?> 

,當我部署到模擬器,它在DDMS中顯示這樣的消息:

「error parsing data org.json.jsonexception value null of org.json.JSONObj ect $ 1無法轉換爲JSONArray「

誰能告訴我爲什麼會發生這種情況?

我真的需要這....因爲這是我最後的項目,我只需要在1天內完成這個......請幫我

謝謝

+0

請不要在新代碼中使用mysql_ *函數。他們不再被維護,並且已經開始了棄用過程。看到紅色框?瞭解準備好的語句,並使用PDO或MySQLi - 本文將幫助您決定哪些。如果您選擇PDO,這裏是一個很好的教程。 – Sergey

+0

[博比表有一個新的最好的朋友](http://xkcd.com/327/) – PeeHaa

回答

0

你的數據獲取從數據庫沒有在UTF-8編碼中,所以json_encode返回null而不是JSON對象。

+0

和我應該寫什麼代碼來獲得JSON對象呢? 你可以請你告訴我該怎麼辦?我將非常感謝它先生感謝 – Safarudin

+0

迭代通過'$行',然後將其添加到'$ output'並使用'iconv'從您的編碼轉換爲UTF- 8 –

0

無論何時解析Json數據,只要確保(檢查)單個/數據數組是否爲空。如果你不檢查它會拋出空指針異常。