2012-11-14 57 views
1

我努力完成我的計劃問心無愧JSON 這是我的計劃JSON和PHP的Android

package udin.tes2; 

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 LihatInfo extends Activity { 

    TextView error,br1; 
    String i,id_kepadatan,returnString; 


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

    @Override 


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

     br1=(TextView)findViewById(R.id.tv_error); 





    } 

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

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

     postParameters.add(new BasicNameValuePair("id_kepadatan", id_kepadatan)); 


/*   String valid = "1";*/  

     String response = null; 

     try { 

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


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

       try{ 
         returnString = ""; 


       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 
           returnString += "\n" + json_data.getString("nama_jalan") + " -> "+ 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{ 


       br1.setText(returnString); 


       } 


       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.kembali2: 
     a = new Intent(this, Menu.class); 
     startActivity(a); 
     break; 
} 
    } 


} 

,這我lihatInfo.php文件

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

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

但是當我部署到模擬器,它說

「錯誤的顯示器!顯示java.lang.NullPointerException」

你能幫助我嗎?我只有2天完成這個 請幫我....謝謝

+0

是你確定要一個int在這裏json_data.getInt(「狀態」) –

+0

我改變它爲字符串,它的工作原理! 但是「顯示錯誤!Java.lang.NullPointerException」呢? ,因爲這是一個問題... 你能幫我嗎? – Safarudin

+0

我不明白怎麼....你能給我一些線索來阻止我使用mysql_function? 以前感謝:) – Safarudin

回答

0

我相信,錯誤就出在這裏

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.getInt("status")+ 
            ", tanggal: "+json_data.getString("tanggal")+ 
            ", waktu: "+json_data.getString("waktu")+ 
            ", keterangan: "+json_data.getString("keterangan") 
         ); 

你可能WANA將其更改爲的getString

+0

謝謝.....我把它改成字符串,它的工作原理! 但我有1個問題更多,「顯示錯誤!Java.lang.NullPointerException」?? 你能幫我嗎? – Safarudin

+0

太棒了,你現在可以接受答案 –

+1

我只是接受答案....謝謝:) – Safarudin