Im new to android programming. So here's the problem. I am currently using a php framework codeigniter to query my data from mysql. When i type the url in my browser i see my json data. But when i retrieving it in my android activity it returns null. Here is my code. php code
function getdrug(){
$id = $_GET['letter'];
//echo $_GET['letter'];
header('Content-type: application/json');
if($id == 'a'){
$this->db->like('Generic_Name','A');
$value['Drugs'] = $this->db->get('drugs')->result();
echo json_encode($value);
}
}
my android activity
ListView lv;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String URL = "http://emedteam-001-site1.ctempurl.com/emed/home/getdrug?letter=a";
jsonobject = JSONfunctions.getJSONfromURL(URL);
Toast.makeText(getApplicationContext(),URL + "\n" +jsonobject, Toast.LENGTH_LONG).show(); }
});
my JSONfunctions
public class JSONfunctions {
public static JSONObject getJSONfromURL(final String URL){
InputStream is = null;
String result = "";
String params = "";
JSONObject jArray = null;
try{
HttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//Convert
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");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
//parse json data
try {
jArray = new JSONObject(result);
System.out.print(jArray);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
here's the link of my url http://emedteam-001-site1.ctempurl.com/emed/home/getdrug?letter=a