我正在尝试从我的数据库中进行搜索,这里是它不断带来以下错误的代码。数据是关于 MySQL 的问题,它一直在崩溃,有人可以帮助我吗?这是我的代码。
public class Main extends MapActivity {
/** Called when the activity is first created. */
private Context ctx = this;
private Context ctx2 = this;
private ProgressDialog pd, pd2 = null;
private Object data, data2 = null;private String url;private MapView myMap;
private Connector http;
static String searchy;
String selection;
// MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMap = (MapView) findViewById(R.id.mapview);
myMap.setBuiltInZoomControls(true);
// addmarkers();
// EditText searchInput = (EditText) findViewById(R.id.txtSearch);
// final String searchitem = searchInput.getText().toString().trim();
Button search = (Button) findViewById(R.id.searchBtn);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Log.i("MyApp", "Background thread starting");
// pd = ProgressDialog.show(Main.this, "Working..",
// "Downloading Data...", true, false);
addmarkers();
}
});
}
//将搜索到的标记添加到地图
public void addmarkers() {
url = "http://10.0.2.2/therm/searchy.php";
http = new Connector(ctx);
http.connect(url, myMap); }
//添加覆盖和从数据库中获取的类连接器
public class Connector {
private Drawable marker;
private GeoPoint point;
private List<Overlay> mapOverlays;
public Connector(Context ctx1) {
ctx = ctx1;
marker = ctx.getResources().getDrawable(R.drawable.default_marker);
itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(
marker, myMap);
}
public void connect(String url, MapView myMap) {
// TODO Auto-generated method stub
InputStream is = null;
// mapOverlays = myMap.getOverlays();
// mapOverlays.clear();
String result = null;
int ct_id;
String ct_name = null;
// String[] ct_namey = null;
String ct_add = null;
String ct_lat;
String ct_long;
String imageURL;
// the year data to send
EditText txt_username = (EditText) findViewById(R.id.txtSearch);
String woi = txt_username.getText().toString();
Toast.makeText(getBaseContext(), woi, Toast.LENGTH_SHORT).show();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("searchy", woi));
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Log.v("log_tag", "Line reads: " + line);
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","address: " + json_data.getString("address")
);
if (searchy.equals(json_data.getString("address"))) {
ct_id = json_data.getInt("id");
ct_name = json_data.getString("name");
// ct_namey[i] = json_data.getString("name");
ct_add = json_data.getString("address");
ct_lat = json_data.getString("lat");
ct_long = json_data.getString("lng");
imageURL = json_data.getString("imageUrl");
Double lon = (Double.parseDouble(ct_long));
Double lat = (Double.parseDouble(ct_lat));
point = new GeoPoint((int) (lat * 1E6),
(int) (lon * 1E6));
CustomOverlayItem overlayItem = new CustomOverlayItem(
point, ct_name, ct_add, imageURL);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
myMap.postInvalidate();
} else {
Toast.makeText(getBaseContext(),
"No such address is found", Toast.LENGTH_LONG)
.show();
}
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return;
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
// .php 文件
<?php
mysql_connect("localhost","root","");
mysql_select_db("hospitals");
$q=mysql_query("SELECT * FROM hosi WHERE address like '%".$_REQUEST['searchy']."%'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();?>
这是我得到的错误:
//error04-06 16:04:07.162: V/log_tag(10708): Line reads: [{"id":"1","name":"The Nairobi Hospital","address":"Upperhill","consult":"2000","phone":"+254202846000","desc":"The.... "}]
04-06 16:04:07.162: V/log_tag(10708): Line reads:
04-06 16:04:07.502: I/log_tag(10708): address: Upperhill
04-06 16:04:07.552: W/dalvikvm(10708): threadid=1: thread exiting with uncaught exception (group=0x400287f0)
04-06 16:04:07.572: E/AndroidRuntime(10708): FATAL EXCEPTION: main
04-06 16:04:07.572: E/AndroidRuntime(10708): java.lang.NullPointerException
04-06 16:04:07.572: E/AndroidRuntime(10708): at com.realestatefinder.plotjson.Main$Connector.connect(Main.java:276)
04-06 16:04:07.572: E/AndroidRuntime(10708): at com.realestatefinder.plotjson.Main$1.onClick(Main.java:86)
04-06 16:04:07.572: E/AndroidRuntime(10708): at android.view.View.performClick(View.java:2408)
04-06 16:04:07.572: E/AndroidRuntime(10708): at android.view.View$PerformClick.run(View.java:8816)
04-06 16:04:07.572: E/AndroidRuntime(10708): at android.os.Handler.handleCallback(Handler.java:587)
04-06 16:04:07.572: E/AndroidRuntime(10708): at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 16:04:07.572: E/AndroidRuntime(10708): at android.os.Looper.loop(Looper.java:123)
04-06 16:04:07.572: E/AndroidRuntime(10708): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-06 16:04:07.572: E/AndroidRuntime(10708): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 16:04:07.572: E/AndroidRuntime(10708): at java.lang.reflect.Method.invoke(Method.java:521)