error in connecting to the server - android

I created a database on cpanel and i choosed a name and pass for it.
So i declared "database","password","user name","localhost" in services.php.
Services.php:
<?php
if (isset($_REQUEST['action'])){
$foo = $_REQUEST['action'];
}else{
echo "Invalid Data";
exit;
}
/*************************************************/
function connectToDatabase()
{
$servername ="localhost";
$username = "us_utab";
$password = "sk3%CkvH";
$dbname = "us_utab";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
/*************************************************/
if ($foo == 'search')
{ search(); }
if ($foo == 'read')
{ read();}
if ($foo == 'insert')
{ insert();}
.
.
.
I changed my android code.
HttpPost method = new HttpPost("http://192.168.1.2/test-website/services.php?action=read");
To :
HttpPost method = new HttpPost("https://utab.com:2083/services.php?
action=read");
Is this correct?
It is not need to use database name , password , user name in android place?
It does not connect to server . so I deleted port 2083 and I am encountering error in below :
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

Well your code seems to be correct and not the problem in this case.
It is not need to use database name , password , user name in android place?
Nope you don't have to if the PHP file already contains these values.
The actual error we are seeing is a SSLPeerUnverifiedException which means that the Certificate of https://utab.com has some issues. So you need to check on that. Also check the date of your device, is it correct?

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
That means you are trying to do a secure https connection to a http server that doesn't expect for secure connections.
$servername ="localhost";
$username = "us_utab";
$password = "sk3%CkvH";
$dbname = "us_utab";
Is that the only contents of services.php ? If yes then it lacks all the code to connect to the database, get arguments from the http request, make probably some database query and return some response. It seems that if the only one argument is action=read it is not clear what it must respond.
UPDATE
After reading your comment, if the server is accessible from the same computer but not from another computer then you have an IP routing problem, not a programming problem. May the computer on the server resides has a firewall that blocks incoming connections, or its network interface is misconfigured.
Take also in account that inside a LAN the public IP with which the router connects to the WAN is not accessible from the LAN computers. For testing inside the LAN you have to use the private LAN IPs, for example 192.168.1.2, not "utab.com"

Related

How put the correct "url" for a getConnection with DriverManager?

I am trying to connect to a MySQL database in an Android application but am unable to do so.
The URL I'm using for the connection to a MySQL database is: jdbc:mysql://xx.xx.x.xxx/dbname, where xx.xx.x.xxx represents the IP address of my server. The database dbname is managed by phpMyAdmin on this server.
But I always receive the same error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I have the internet PERMISSION in my manifest.
The code I'm using:
private void callSQL() {
java.sql.Connection laConnection;
java.sql.Statement transmission;
ResultSet leResultat;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
laConnection= DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx/dbname", "user", "passwrd");
transmission = laConnection.createStatement();
leResultat= transmission.executeQuery("select * from Communes");
while (leResultat.next()) {
System.out.println("Commune : "+ leResultat.getString("Nom"));
}
}catch(Exception e){
System.out.println(e);
}
}
I have been able to use another solution with a HTTP request on a PHP page that connects to the database. This solution works well, but which one is the best way to use?
I found the solution. The problem wasn't the code. The problem is the connection. I hadn't the permission to connect the database. With the permission allowed to a distant user on the database, it was OK.

Challenge with Android communicating to GAE using restlet

I'm using Eclipse to (try to) build an Android client to get reuse a restlet service I developed using GAE and GWT.
My service is running at
http://127.0.0.1:8888/abc/audits
I can test this by going directly to this url, and by using my GWT client - both work.
However, the following code returns a communication error
private AuditsResource resource;
private Audits audits;
ClientResource cr = new ClientResource("http://127.0.0.1:8888/abc/audits");
resource = cr.wrap(AuditsResource.class);
try {
// Get the remote contact
audits = resource.retrieve();
// The task is over, let the parent conclude.
handler.sendEmptyMessage(0);
} catch (Exception e) {
Message msg = Message.obtain(handler, 2);
Bundle data = new Bundle();
data.putString("msg", "Cannot get the contact due to: "
+ e.getMessage());
msg.setData(data);
handler.sendMessage(msg);
}
I have no idea where to look next. I've instrumented the server implementation of AuditsResource, and it is never touched (by the Android application).
The AuditsResource class has one method, to keep things simple for now
#Get
public Audits retrieve();
It appears the problem is that the Andriod Emulator cannot connect to either 127.0.0.1 or 10.0.2.2. The solution is to use your PC's IP address.
I am able to connect from Android to my local Google App Engine through Android/Restlet using 10.0.2.2 instead of localhost.

getjson response returns null in phonegap (android)

$.getJSON("http://localhost:8080/v1/message",
function(data) {
console.log(data);
alert("message"+data);//this alert stmt is displayed it means that
the service call is getting executed
});
service method
#RequestMapping(method = RequestMethod.GET,
value = "/message" )
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public String message()
{
return "jasdknsd";
}
I am getting response of this call as null please help me out if I am doing something wrong in the service method and I have both of them in same network because of which I am not using callback
Connection to localhost is not supported.
http://code.google.com/p/android/issues/detail?id=133
127.0.0.1 is the emulated system's own loopback interface, not the one running on
your host development machine.
within the Android system, one should use 10.0.2.2 which is an alias specifically
setup to contact your host 127.0.0.1
use 10.0.2.2 instead of localhost:8080

Recommended way to sync information between devices

I'm currently developing and I came up to the following question:
How can/should I store basic information, such as unlocked items and levels between devices.
Of course, I store them within preference files.
But what if the user buys a new phone and wants to continue playing on there?
(Shared-)Preferences won't be duplicated.
Thus, is there any (from google) recommended way (cloud?) to solve this issue?
I'm no expert, but the way I do this is with an online SQL database. Have the device connect to it, and save/load any data you need. I know this will not work with files, only text-based information. I also do this using Flash. If this is your route I can share some code.
Ok, so you need 3 things if you are doing this in flash: Flash (duh), PHP files, and a SQL server (I have a MYSQL server).
How it will work is flash will load php, php will do it's thing (like making changes to the database) and spit back it's results to flash.
Let's say we want to get all usernames from the database.
Database
The table name is called "Players" with a single column called "u_id"
Flash
var loader:URLLoader; //makes a loader that will "load" the php page
public function DoIt()
{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("http://yourDomain.com/thePHPFile.php?arg=42"));
}
public function loaderComplete(e:Event):void
{
var returnXML:XML; //we are assuming the return values are in xml format, trust me, this might be easier
for each (var ListItem:XML in returnXML..Users) //you might need to change this "users" to be the root of the xml file
{
var userName:String = ListItem.u_id.toString();
//you can do what you want with the data here
}
}
PHP
Now it might not be required, but I try to make sure the PHP files and the DB is on the same host, so that I can do "localhost" as the host name
<?php
$argumentVar = $_GET["arg"]; //not using this, just showing it's possible
$connect = mysql_connect("localhost", "db_username", "db_password");
if(!$connect)
{
die("error connecting to DB" . mysql_error());
}
$db_selected = mysql_select_db("the_name_of_the_db", $connect);
if(!$db_selected)
{
die("error selecting db" . mysql_error());
}
$table_name = "Players" //this is the name of your database
$sql = "SELECT * FROM $table_name;"; //not sure if you need ending ';'
$dbresult = mysql_query($sql, $connect); //this is where all your data comes into
//and now to save all the information to a xml file
$doc = new DOMDocument('1.0');
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($dbresult))
{
$occ = $doc->createElement($table_name);
$occ = $root->appendChild($occ);
foreach ($row as $fieldname => $fieldvalue)
{
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}
$xml_string = $doc->saveXML();
echo $xml_string;
?>
Ok, go play with that, it should start you on the right path.
i think the best way would be to have an online database be maintained which is regularly updated with the latest information for every registered user. So instead of storing everything locally it can be stored on a remote server and sent to the user as and when required. so as long as the user owns the account created by himself for the application, no data will be lost.

Android org.apache.hhtp.NoHttpRsponseException:The target server failed to respond error on login screen

I've created a login screen but when I put the username and password in it displays 'org.apache.hhtp.NoHttpRsponseException:The target server failed to respond' within the android emulator.
I'm new to android and so i'm not sure how to fix this.
It's not showing any errors in eclipse so could my php file be the problem?
Here's my code
<?php
$un=$_POST['username'];
$pw=$_POST['password'];
$conn = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('app');
$query = 'SELECT * FROM login WHERE username = ‘$un’ AND password = ‘$pw’';
$result = mysql_query($query) or die ('Unable to verify user because : ' .
mysql_error());
if (mysql_num_rows($result) > 0) {
echo 1;
}
else {
// print status message
echo 0;
}
?>
Any help would be much appreciated
thanks
I am not completely sure what your intent is with the above code but one thing to keep in mind is that your development machine and the android emulator have different IP addresses. They are two different 'machines'. In which case the use of localhost may not be correct.

Categories

Resources