2012-12-12 63 views
0

我想从我的Android手机连接到我的服务器超时。但我得到了超时连接错误。 可能是什么原因?连接连接到服务器时,从Android设备

我的代码是

public void onClick(View view) { 
    if (view == loginBtn) { 
     if (username.getText().length() != 0 & password.getText().length() != 0) { 

      progressDialog = ProgressDialog.show(this,Farsi.Convert(""),"Verifying user credential"); 

      Thread thread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         Connection connection = getConnection(); 
         User user = connection.login(username.getText().toString(), password.getText().toString()); 
         if (user != null) { 
          BaseKaizenActivity.getStorageManager().setUser(user); 
          Log.d("---", user.getId() + " : " + user.getName()); 
          showActivity(MainMenuActivity.class); 
         } 
         else { 
          handleException("Invalid username or password"); 
         } 
        } 
        catch (Exception exc) { 
         handleException(exc.getMessage()); 
        } 
        finally { 
         if (progressDialog != null) { 
          progressDialog.dismiss(); 
         } 
        } 
       } 
      }); 
      thread.start(); 
     } 
     else { 
      handleException("Insert username and password"); 
     } 
    } 
} 

我得到的例外是

Connect to /10.0.2.2:8080 timed out 
org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.2.2:8080 timed out 
at  org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121) 
at  org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(
     DefaultClientConnectionOperator.java:156) 
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
at  org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
at com.pda.kaizen.ConnectionImpl.executeHttpPost(ConnectionImpl.java:133) 
at com.pda.kaizen.ConnectionImpl.login(ConnectionImpl.java:88) 
at com.pda.kaizen.activity.LoginActivity$1.run(LoginActivity.java:98) 
at java.lang.Thread.run(Thread.java:1019) 
+0

是在你的manifest文件中加入互联网的权限? –

+0

你可以把你的代码? –

回答

2

试试这个示例代码:

public class AndroidClient extends Activity { 

EditText textOut; 
TextView textIn; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    textOut = (EditText)findViewById(R.id.textout); 
    Button buttonSend = (Button)findViewById(R.id.send); 
    textIn = (TextView)findViewById(R.id.textin); 
    buttonSend.setOnClickListener(buttonSendOnClickListener); 
} 

Button.OnClickListener buttonSendOnClickListener 
= new Button.OnClickListener(){ 

@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 
Socket socket = null; 
DataOutputStream dataOutputStream = null; 
DataInputStream dataInputStream = null; 

try { 
    socket = new Socket("192.168.1.101", 8888); 
    dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
    dataInputStream = new DataInputStream(socket.getInputStream()); 
    dataOutputStream.writeUTF(textOut.getText().toString()); 
    textIn.setText(dataInputStream.readUTF()); 
} catch (UnknownHostException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
finally{ 
    if (socket != null){ 
    try { 
    socket.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 

    if (dataOutputStream != null){ 
    try { 
    dataOutputStream.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 

    if (dataInputStream != null){ 
    try { 
    dataInputStream.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 
} 
}}; 
} 

和你的XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
/> 
<EditText 
android:id="@+id/textout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
<Button 
android:id="@+id/send" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Send" 
/> 
<TextView 
android:id="@+id/textin" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/> 
</LinearLayout> 

firts在添加此每次任务您的清单

<uses-permission android:name="android.permission.INTERNET"/> 
+0

当我使用模拟器时,我的代码变得很糟糕。只有当我使用Android设备,我得到这个超时错误。一个问题,我可以使用我的android设备连接到10.0.2.2或没有 – ZAJ

+1

'10.0.2.2'我认为这不是一个有效的地址使用地址,就像我在上面的代码中提到的。为了让你的计算机地址转到控制台,如果使用Windows并编写'ipconfig',你会看到可能的东西,从哪里你可以看到一个像这样的IPv4地址字符串。 。 。 。 。 。 。 。 。 。 。 :192.168.2.147'使用这个IP是你的代码中的服务器地址。 –