Android : Retriving data in android application

on Friday, April 3, 2015


I have developed android application of company , whithin this application I have interface to retrive an employee information , but everytime I run the program it didn't retrive any data , and give me the code that I set when fetching the data from the database is not successful ,


here is my code



public class Search2 extends ActionBarActivity {
InputStream is=null;
String result=null;
String line=null;
int code;
TextView name;
TextView jobtitle;
TextView eID;
TextView email;
TextView phone;
TextView address;


String IDEmployee;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search2);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

name=(TextView)findViewById(R.id.Empname);
eID=(TextView)findViewById(R.id.EmpJb);
jobtitle=(TextView)findViewById(R.id.EmpID);
email=(TextView)findViewById(R.id.Empmail);
phone=(TextView)findViewById(R.id.Empphone);
address=(TextView)findViewById(R.id.Empaddress);


IDEmployee = "2120008";
getData();
}
public void getData() {


ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("IDEmployee",IDEmployee));

try{

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://oilspotmobileapplication.webuda.com/employees.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");





}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}



try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");

}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}

try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));

if(code==1)
{
Toast.makeText(getBaseContext(), "Information Retrieved Successfully",
Toast.LENGTH_SHORT).show();



name.setText((json_data.getString("FirstName")));
eID.setText((json_data.getString("IDEmployee")));
email.setText((json_data.getString("EmployeeEmail")));
phone.setText((json_data.getString("Phone")));
address.setText((json_data.getString("PostCode")));
jobtitle.setText((json_data.getString("JobTitle")));

}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again Later",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}







}}


And This is the php page



<?php

if(! $conn)
{
die('Could not connect: '.mysql_error());
}
mysql_select_db("a2202757_OURDATA",$conn);

$response = array();



$sql=mysql_query("select IDEmployee,FirstName,JobTitle,EmployeeEmail,Phone,PostCode from employee where IDEmployee='".$_POST['IDEmployee']."'");


if (mysql_num_rows($sql) > 0) {
// looping through all results
// products node


while ($row = mysql_fetch_array($sql)) {
// temp user array


$response["IDEmployee"] = $row["IDEmployee"];
$response["FirstName"] = $row["FirstName"];
$response["JobTitle"] = $row["JobTitle"];
$response["EmployeeEmail"] = $row["EmployeeEmail"];
$response["Phone"] = $row["Phone"];
$response["PostCode"] = $row["PostCode"];






}
// success
$response['code']=1;

// echoing JSON response
echo json_encode($response);

}

else {
// no products found
$response['code']=0;


// echo no users JSON
echo json_encode($response);

}

mysql_close();
?>


This is the message that always shown to me when I run the application


Toast.makeText(getBaseContext(), "Sorry, Try Again Later", Toast.LENGTH_LONG).show();


What is the problem ??


0 comments:

Post a Comment