Hello everyone,
I am trying to connect my android application to MYSQL database. I have used the following:
MYSQL already created database test (accessed via xampp using localhost/phpmyadmin)
Created a table named as student in the database test consisting of Name and Roll No columns and 2 records.
A user on the database with credentials username: rubeea password 123
Then I created a php script as follows:
<?php
mysql_connect("localhost","rubeea","123");//change server name //pass username according your settings
mysql_select_db("test");// also chang the Mysql database name
$sql1=mysql_query("select * from student");
if (!$sql1) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
while($row=mysql_fetch_assoc($sql1))
$output[]=$row;
echo json_encode($output);// this will print the output in json
mysql_close();
?>
When I run this php file from my browser through localhost/mysqlchk.php. It shows me the correct data in the json format in the browser.
Then I wrote the android app as:
MainActivity:
import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.concurrent.ExecutionException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import net.sourceforge.jtds.jdbc.*;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
private EditText usernameField,passwordField;
private TextView status,role,method;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.textView6);
role = (TextView)findViewById(R.id.textView7);
method = (TextView)findViewById(R.id.textView9);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void login(View view){
method.setText("Get Method");
new SigninActivity(this,status,role,0).execute("abc","123");
}
}
SigninActivity:
package com.example.sqlserverexample;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class SigninActivity extends AsyncTask<String,Void,String>{
private TextView statusField,roleField;
private Context context;
private int byGetOrPost = 0;
//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,
TextView roleField,int flag) {
this.context = context;
this.statusField = statusField;
this.roleField = roleField;
byGetOrPost = flag;
}
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0) {
String result="";
if(byGetOrPost == 0){ //means by Get Method
try{
String link = "http://192.168.1.2:80/mysqlchk.php";
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line="";
while ((line = in.readLine()) != null) {
sb.append(line+"\n");
//break;
}
in.close();
result= sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
return result;
}
}
@Override
protected void onPostExecute(String result){
this.statusField.setText("result"+"\t"+result);
this.roleField.setText("ck");
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +"Name : "+json.getString("Name")+" "+json.getString("Roll"); }
this.roleField.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}
Where 192.168.1.2:80 is the ip of my machine connected to wifi network. Now, when I try to run this application on my android device (Nokia X) with xampp server on (MYSQL and Apache running on it), it shows the message
Exception: Connection refused to http:\192.168.1.2:80.... when printed in postExecute method.
I don't know why it is showing this error. Please help!!