url failed 这个单词failed是什么意思思?

Connect Android to MS SQL Database. & ParallelCodes();
In this post we will be seeing how you can connect android application to your Microsoft server Database.
For connecting with MS SQL server we will require the .
Add this as library to your project.
Couple of things first :
Your firewall must be configured to allow the connection to database (the code might not work if it’s not configured). For more on this please refer the
Your MS SQL database must be configured to allow remote connection. A simple way to find is connecting the object explorer through the IPv4 address of your PC. If you are using MS SQL Server 2005, I have made a
Software Used :
Making the Database :
Create a new Database named Andro.
Below is the script which will create two tables :
Producttbl
Script for Database
select * from Usertbl
create table Usertbl
Id int identity (1,1) not null,
UserId nvarchar(50) not null,
Password nvarchar(50) not null
insert into Usertbl (UserId,Password) values ('hitesh1120', '789')
create table Producttbl (
Id int identity (1,1) not null,
ProName nvarchar(50) not null,
ProDesc nvarchar(50) not null,
OnDate nvarchar(50) not null
truncate table Producttbl
insert into Producttbl (ProName,ProDesc,OnDate) values ('Samsung J7', 'Gamers targeted mobile from Samsung', ' 19:56:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Samsung J5', 'Gamers targeted mobile from Samsung', ' 19:56:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Xiaomi Redmi 2', 'Best Entry level smartphone from Xiaomi', ' 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Apple iPhone 6s', 'Flagship device from APPLE', ' 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Apple iPhone 6s Plus', 'Flagship device from APPLE', ' 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Xiaomi Redmi Note 2', 'Best Entry level smartphone from Xiaomi', ' 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Lenovo A6000', 'Best Entry level smartphone from Lenovo', ' 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Motorola MOTO X Pure Edition', 'Best Android device right now!!', ' 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Motorola MOTO E 2gen', 'Budget device from Motorola', ' 19:59:00.000')
insert into Producttbl (ProName,ProDesc,OnDate) values ('Motorola MOTO E 2gen 4G', 'Budget device from Motorola', ' 19:59:00.000')
select * from Producttbl
Creating the Application :
Now in Android Studio create a new project. You can select target version and compile with version according to your requirements. I selected minimum version as 2.1.
Open the activity_main.xml file and edit it as below :
activity_main.xml :
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#282828"
android:padding="10dp"&
&LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:padding="10dp"&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SQL APP"
android:typeface="sans"
android:textSize="35sp"
android:textColor="#00aa55"
android:textStyle="bold"
android:gravity="center" /&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:textColorHint="#ffffff"
android:background="#5d5d5d"
android:padding="10dp"
android:hint="USER ID"
android:textSize="20sp"
android:id="@+id/edtuserid" /&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="PASSWORD"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:textStyle="bold"
android:background="#5d5d5d"
android:padding="10dp"
android:inputType="textPassword"
android:layout_marginTop="5dp"
android:id="@+id/edtpass" /&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btnbg"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp"
android:padding="10dp"
android:layout_marginTop="10dp"
android:id="@+id/btnlogin"
android:text="Login" /&
&ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/pbbar" /&
&/LinearLayout&
&/RelativeLayout&
This will design our layout as below :
Now lets program our application to connect with our database.
Coding the application :
library file and add it to our application. This library file helps in connecting ANDROID and MS SQL Database.
After this, create a new class file with name ConnectionClass.java and declare following string variables :
ConnectionClass.java
public class ConnectionClass {
String ip = "192.168.0.100";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "Andro";
String un = "hitesh";
String password = "789";
ip – The IP address or server name of your PC or server where your database is stored.
classs – Class name.
db – Database name.
un – Username of the database (yours will be different).
password – Password of the database.
Add this method in this class.
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn =
String ConnURL =
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
This will return the Connection object for connecting with the server.
Complete file :
package hitesh.
import android.annotation.SuppressL
import android.os.StrictM
import android.util.L
import java.sql.SQLE
import java.sql.C
import java.sql.DriverM
* Created by h-pc on 16-Oct-15.
public class ConnectionClass {
String ip = "192.168.0.100";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "Andro";
String un = "hitesh";
String password = "789";
@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn =
String ConnURL =
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
Now open the MainActivity.java class file and declare following variables :
ConnectionClass connectionC
EditText edtuserid,
In the onCreate method initialize this variables :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionClass = new ConnectionClass(); &//the class file
edtuserid = (EditText) findViewById(R.id.edtuserid);
edtpass = (EditText) findViewById(R.id.edtpass);
btnlogin = (Button) findViewById(R.id.btnlogin);
pbbar = (ProgressBar) findViewById(R.id.pbbar);
pbbar.setVisibility(View.GONE);
Now add onClick listener to btnlogin button
btnlogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DoLogin doLogin = new DoLogin(); &// this is the Asynctask
doLogin.execute("");
Now make a new AsyncTask method DoLogin as following
public class DoLogin extends AsyncTask&String,String,String&
String z = "";
Boolean isSuccess =
String userid = edtuserid.getText().toString();
String password = edtpass.getText().toString();
protected void onPreExecute() {
pbbar.setVisibility(View.VISIBLE);
protected void onPostExecute(String r) {
pbbar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();
if(isSuccess) {
Intent i = new Intent(MainActivity.this, AddProducts.class);
startActivity(i);
protected String doInBackground(String... params) {
if(userid.trim().equals("")|| password.trim().equals(""))
z = "Please enter User Id and Password";
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
String query = "select * from Usertbl where UserId='" + userid + "' and password='" + password + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
z = "Login successfull";
isSuccess=
z = "Invalid Credentials";
isSuccess =
catch (Exception ex)
isSuccess =
z = "Exceptions";
The doInBackground method does everything over here.
Boolean isSuccess flag changes depending upon the status of the login is successful or not.
String z is used for showing the returning message to the user.
ResultSet is used for confirming whether the user exist with the respective User ID and Password.
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
z = "Login successfull";
isSuccess=
z = "Invalid Credentials";
isSuccess =
Here’s complete file :
MainActivity.java :
package hitesh.
import android.app.A
import android.content.I
import android.os.AsyncT
import android.os.B
import android.view.V
import android.widget.B
import android.widget.EditT
import android.widget.ProgressB
import android.widget.T
import java.sql.C
import java.sql.ResultS
import java.sql.S
public class MainActivity extends Activity {
ConnectionClass connectionC
EditText edtuserid,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionClass = new ConnectionClass();
edtuserid = (EditText) findViewById(R.id.edtuserid);
edtpass = (EditText) findViewById(R.id.edtpass);
btnlogin = (Button) findViewById(R.id.btnlogin);
pbbar = (ProgressBar) findViewById(R.id.pbbar);
pbbar.setVisibility(View.GONE);
btnlogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DoLogin doLogin = new DoLogin();
doLogin.execute("");
public class DoLogin extends AsyncTask&String,String,String&
String z = "";
Boolean isSuccess =
String userid = edtuserid.getText().toString();
String password = edtpass.getText().toString();
protected void onPreExecute() {
pbbar.setVisibility(View.VISIBLE);
protected void onPostExecute(String r) {
pbbar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();
if(isSuccess) {
Intent i = new Intent(MainActivity.this, AddProducts.class);
startActivity(i);
protected String doInBackground(String... params) {
if(userid.trim().equals("")|| password.trim().equals(""))
z = "Please enter User Id and Password";
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
String query = "select * from Usertbl where UserId='" + userid + "' and password='" + password + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
z = "Login successfull";
isSuccess=
z = "Invalid Credentials";
isSuccess =
catch (Exception ex)
isSuccess =
z = "Exceptions";
AndroidManisfest.xml
&?xml version="1.0" encoding="utf-8"?&
&manifest xmlns:android="/apk/res/android"
package="hitesh.sqlapp" &
&uses-permission android:name="android.permission.INTERNET" /&
&uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&
&uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&
&application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" &
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"&
&intent-filter&
&action android:name="android.intent.action.MAIN" /&
&category android:name="android.intent.category.LAUNCHER" /&
&/intent-filter&
&/activity&
android:name=".AddProducts"
android:label="@string/app_name"
android:screenOrientation="portrait"/&
&/application&
&/manifest&
Do you have any questions or queries on this post…please comment below and let me know what you think about this code.
More on Android and MS SQL :
Pingback: ()
Pingback: ()
Pingback: ()
Pingback: ()
Pingback: ()
Pingback: ()
Pingback: ()
Pingback: ()
Pingback: ()
Pingback: ()
Popular Posts
Like us on Facebokk
If you are using Adblock, please disable it to help us keep this website free for all. Ads are single source of revenue for us}

我要回帖

更多关于 failed for url 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信