0

I set a button in an Android program to connect to .net PC.

Here is my code:

public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v==btn_con){
        try{
            s=new Socket("xxx.xxx.xxx.xxx", xxxx);
            BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
            String str=in.readLine();
            Toast t=Toast.makeText(this, str, Toast.LENGTH_LONG);
            t.show();
            _isconnected=true;
        }catch(Exception e){
           Toast t=Toast.makeText(this, "链接发生错误"+e.getMessage(), Toast.LENGTH_LONG);
           t.show();
           System.out.println("连接发生错误:"+e.getMessage());
        }
    }

When I run the program it throws a Permission denied exception. I have run a .net service program; this is a client Android program. If I run a Java client it has no such problem. When I run the Android program in my phone, I don't know how to solve the problem.

john_science
  • 6,325
  • 6
  • 43
  • 60
Mryoun
  • 153
  • 1
  • 3
  • 10

1 Answers1

1

Maybe it's because you forget to grant full network access permission in your Android manifest file?

See What permission do I need to access Internet from an android application?

Community
  • 1
  • 1
TactMayers
  • 884
  • 8
  • 22
  • now i got a expection "connect reset by peer" ,what should i do? – Mryoun Sep 20 '12 at 08:20
  • get connect refused expection,how to set it? – Mryoun Sep 20 '12 at 08:37
  • The "connection reset by peer", "connection refused" are network related exceptions. These means the Android permission problem is gone, but you're now having problems with network communication between your device and the server. You should check if your server is up and running, your device is connected to network properly, there's no firewall blocking the connection, etc... – TactMayers Sep 20 '12 at 09:20