代码
private static final String TAG_SUCCESS = "success";
String URL_SERVER = "http://localhost/italk/servlet/login.ValidateUser";
//String URL_SERVER = "http://localhost/italk/servlet/login.ValidateUser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputUserName = (EditText) findViewById(R.id.txtUserName);
inputPassWord = (EditText) findViewById(R.id.txtPassWord);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Neu login ->true
new doLogin().execute();
}
});
}
class doLogin extends AsyncTask<String, String, String> {
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
String success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_name", inputUserName.getText().toString()));
params.add(new BasicNameValuePair("pass_word", inputPassWord.getText().toString()));
params.add(new BasicNameValuePair("db_name", "jdbc/ITTALKDS"));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
URL_SERVER, "GET", params);
// check your log for json response
//Log.d("Single Product Details", json.toString());
// json success tag
success = json.getString("result");
if (success.equals("2")) {//Login successfully
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Login successfully...");
Intent i = new Intent(getApplicationContext(), AlliTalk.class);
startActivity(i);
} else {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Login failt...");
}
} catch (JSONException e) {
e.printStackTrace();
}
});
return null;
}
}
错误信息:
06-24 23:17:01.946: E/AndroidRuntime(7968): FATAL EXCEPTION: main
06-24 23:17:01.946: E/AndroidRuntime(7968): android.os.NetworkOnMainThreadException
06-24 23:17:01.946: E/AndroidRuntime(7968): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-24 23:17:01.946: E/AndroidRuntime(7968): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-24 23:17:01.946: E/AndroidRuntime(7968): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-24 23:17:01.946: E/AndroidRuntime(7968): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-24 23:17:01.946: E/AndroidRuntime(7968): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-24 23:17:01.946: E/AndroidRuntime(7968): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-24 23:17:01.946: E/AndroidRuntime(7968): at java.net.Socket.connect(Socket.java:842)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-24 23:17:01.946: E/AndroidRuntime(7968): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-24 23:17:01.946: E/AndroidRuntime(7968): at com.italk.JSONParser.makeHttpRequest(JSONParser.java:64)
06-24 23:17:01.946: E/AndroidRuntime(7968): at com.italk.MainActivity$doLogin$1.run(MainActivity.java:74)
06-24 23:17:01.946: E/AndroidRuntime(7968): at android.os.Handler.handleCallback(Handler.java:725)
06-24 23:17:01.946: E/AndroidRuntime(7968): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 23:17:01.946: E/AndroidRuntime(7968): at android.os.Looper.loop(Looper.java:137)
06-24 23:17:01.946: E/AndroidRuntime(7968): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-24 23:17:01.946: E/AndroidRuntime(7968): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 23:17:01.946: E/AndroidRuntime(7968): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 23:17:01.946: E/AndroidRuntime(7968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 23:17:01.946: E/AndroidRuntime(7968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 23:17:01.946: E/AndroidRuntime(7968): at dalvik.system.NativeStart.main(Native Method)