我努力完成我的項目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天內完成這個......請幫我
謝謝
請不要在新代碼中使用mysql_ *函數。他們不再被維護,並且已經開始了棄用過程。看到紅色框?瞭解準備好的語句,並使用PDO或MySQLi - 本文將幫助您決定哪些。如果您選擇PDO,這裏是一個很好的教程。 – Sergey
[博比表有一個新的最好的朋友](http://xkcd.com/327/) – PeeHaa