1

我尝试用 JSON 这个我的程序完成我的程序

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 天完成这个请帮帮我....谢谢

4

1 回答 1

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")
                          );

你可能想把它改成 getString

于 2012-11-14T19:21:01.260 回答