i have searched each and every thing at this site but still i don't understand what is matter with my code i have debugged it also it goes to exception when it tries to execute the this line "HttpResponse response=httpclient.execute(httppost)" i have also added permission of internet in android manifest file but still don't understand whats the matter.
please help me i shall be very thankful to all of you.....
here is my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// set a click listener on the alert button
Button alert = (Button) findViewById(R.id.alertbtn);
alert.setOnClickListener(this);
}
public void onClick(View view)
{
if (view == findViewById(R.id.alertbtn))
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(2);
postParameters.add(new BasicNameValuePair("email","awais"));
postParameters.add(new BasicNameValuePair("password","123"));
try
{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://localhost/doctroidlogin.php");
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
if(entity.toString().length()>0)
alertbox.setMessage("Welcome to DangerZone!");
else
alertbox.setMessage("Invalid Email or Password");
alertbox.show();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
开发者_Go百科 catch (IOException e)
{
e.printStackTrace();
}
Log Cat
09-23 00:53:51.251: WARN/System.err(327): java.net.UnknownHostException: pannl.com
09-23 00:53:51.266: WARN/System.err(327): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
09-23 00:53:51.285: WARN/System.err(327): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
09-23 00:53:51.285: WARN/System.err(327): at java.net.InetAddress.getAllByName(InetAddress.java:256)
09-23 00:53:51.306: WARN/System.err(327): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
09-23 00:53:51.316: WARN/System.err(327): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-23 00:53:51.336: WARN/System.err(327): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-23 00:53:51.356: WARN/System.err(327): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
09-23 00:53:51.366: WARN/System.err(327): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-23 00:53:51.375: WARN/System.err(327): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-23 00:53:51.386: WARN/System.err(327): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-23 00:53:51.406: WARN/System.err(327): at huuah.dialogboxes.LastActivity.onClick(LastActivity.java:57)
09-23 00:53:51.415: WARN/System.err(327): at android.view.View.performClick(View.java:2485)
09-23 00:53:51.436: WARN/System.err(327): at android.view.View$PerformClick.run(View.java:9080)
09-23 00:53:51.466: WARN/System.err(327): at android.os.Handler.handleCallback(Handler.java:587)
09-23 00:53:51.476: WARN/System.err(327): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 00:53:51.486: WARN/System.err(327): at android.os.Looper.loop(Looper.java:123)
09-23 00:53:51.506: WARN/System.err(327): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-23 00:53:51.516: WARN/System.err(327): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 00:53:51.526: WARN/System.err(327): at java.lang.reflect.Method.invoke(Method.java:507)
09-23 00:53:51.546: WARN/System.err(327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-23 00:53:51.566: WARN/System.err(327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-23 00:53:51.566: WARN/System.err(327): at dalvik.system.NativeStart.main(Native Method)
http://localhost
implies that you are running server on your device (or emulator). Are you? I guess not since it's php.
You have to use a real IP or domain name of the server.
OTOH if you are trying this on emulator and server is on your host machne then you need to set a few parameters: http://juristr.com/blog/2009/10/accessing-host-machine-from-your/
精彩评论