Reputation: 67
I am trying to make a simple android app to connect to localhost database and retrieve data from the same.
I use volley library and my php scripts are working fine. I am using Genymotion emulator. Everytime I launch the application, I get the following error message:"unfortunately, connection has stopped ".
Is it something related to the emulator?
My main activity is:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextId = (EditText)findViewById(R.id.editTextId);
buttonGet = (Button)findViewById(R.id.buttonGet);
textViewResult = (TextView)findViewById(R.id.textViewresult);
buttonGet.setOnClickListener(this);
}
private void getData(){
String id = editTextId.getText().toString().trim();
if(id.equals("")){
Toast.makeText(MainActivity.this, "Please enter an ID", Toast.LENGTH_SHORT).show();
return;
}
loading = ProgressDialog.show(this,"Please wait....","Fetching...",false,false);
String url = Config.DATA_URL+id;
StringRequest stringRequest = new StringRequest(url,new Response.Listener<String>(){
@Override
public void onResponse(String response){
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
String address="";
String director="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
director = collegeData.getString(Config.KEY_DIRECTOR);
} catch (JSONException e){
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"Address:\t"+address+"Director:\t"+director);
}
@Override
public void onClick(View v) {
getData();
}
}
The config file:
public class Config {
public static final String DATA_URL = "http://127.0.0.1/getDatanew.php?id=";
public static final String KEY_NAME = "name";
public static final String KEY_ADDRESS = "address";
public static final String KEY_DIRECTOR = "director";
public static final String JSON_ARRAY = "result";
}
The dataConnect.php file:
<?php
$con = new mysqli("localhost", "root", "", "college") or die ('Unable to Connect');
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>
The getData.php file
<?php
// if($_SERVER['REQUESTED_METHOD']=='GET'){
if ($_GET['id']) {
$id = $_GET['id'];
if ($id)
// echo 'hello '.$id;
require_once('dbConnectnew.php');
// echo $con;
$sql = "SELECT * FROM college WHERE id='" . $id . "'";
$r = mysqli_query($con, $sql);
// if (!$r) {
// printf("Error: %s\n", mysqli_error($con));
// exit();
//}
$res = mysqli_fetch_array($r);
$result = array();
array_push(
$result,
array(
"name" => $res['name'],
"address" => $res['address'],
"director" => $res['director']
)
);
echo json_encode(array("result" => $result));
mysqli_close($con);
} else
echo 'no if';
?>
Upvotes: -1
Views: 275
Reputation: 41
Instead of 127.0.0.1, try putting in the Ip Address of your WiFi or LAN. You can get it by running the ipconfig in cmd.
Upvotes: 0