我的应用程序正在关闭错误,当我尝试连接MySQL服务器(通过xampp)时,请帮助我。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String result =null;
InputStream is=null;
StringBuilder sb =null;
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http:/localhost/db.php");
List<? extends NameValuePair> nameValuePairs = null;
httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response =httpclient.execute(httppost);
HttpEntity entity =response.getEntity();
is=entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in httpconnection"+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine()+ "\n");
String line ="0";
while ((line=reader.readLine())!=null) {
sb.append(line +"\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result"+ e.toString());
}
@SuppressWarnings("unused")
String refno,name;
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(1);
refno =json_data.getString("refno");
name = json_data.getString("name");
}
}catch(JSONException e1){
Toast.makeText(getBaseContext(), "No data",Toast.LENGTH_LONG).show();
}catch(ParseException e1){
e1.printStackTrace();
}
}
}