java - Android Java 应用程序 - java.net.SocketException : Permission denied

标签 java android socketexception adk

为我的 android 应用程序使用 MYSQL:

主要 Activity 。

public class MainActivity extends Activity implements OnClickListener {

Button login;
EditText username, password;
TextView status;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePair;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setup();
}

private void setup() {
    // TODO Auto-generated method stub

    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);
    status = (TextView) findViewById(R.id.status);
    login.setOnClickListener(this); 
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId()) {
    case R.id.login:

        login();

        break;
    }
    }

private void login() {
    // TODO Auto-generated method stub
    try {
        httpclient= new DefaultHttpClient();
        httppost= new HttpPost("http://109.74.245.109/~profiled/android/index.php"); 
    //  Adding Data:
        nameValuePair = new ArrayList<NameValuePair>(1);
    //  Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
        nameValuePair.add(new BasicNameValuePair("username",username.getText().toString().trim()));
        nameValuePair.add(new BasicNameValuePair("password",password.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    // Execute HTTP Post Request
        response = httpclient.execute(httppost);



        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        status.setText(""+response);
        if(response.equalsIgnoreCase("No user found")) {

            startActivity(new Intent(MainActivity.this, UserPage.class));
        }


        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的错误:

05-17 13:39:25.309: W/System.err(292): java.net.SocketException: Permission denied
05-17 13:39:25.318: W/System.err(292):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
05-17 13:39:25.318: W/System.err(292):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
05-17 13:39:25.318: W/System.err(292):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
05-17 13:39:25.318: W/System.err(292):  at java.net.Socket.checkClosedAndCreate(Socket.java:873)
05-17 13:39:25.318: W/System.err(292):  at java.net.Socket.connect(Socket.java:1020)
05-17 13:39:25.333: W/System.err(292):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-17 13:39:25.337: W/System.err(292):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-17 13:39:25.337: W/System.err(292):  at com.profiledt.helloworld.MainActivity.login(MainActivity.java:88)
05-17 13:39:25.337: W/System.err(292):  at com.profiledt.helloworld.MainActivity.onClick(MainActivity.java:70)
05-17 13:39:25.337: W/System.err(292):  at android.view.View.performClick(View.java:2408)
05-17 13:39:25.337: W/System.err(292):  at android.view.View$PerformClick.run(View.java:8816)
05-17 13:39:25.337: W/System.err(292):  at android.os.Handler.handleCallback(Handler.java:587)
05-17 13:39:25.337: W/System.err(292):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 13:39:25.357: W/System.err(292):  at android.os.Looper.loop(Looper.java:123)
05-17 13:39:25.357: W/System.err(292):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-17 13:39:25.357: W/System.err(292):  at java.lang.reflect.Method.invokeNative(Native Method)
05-17 13:39:25.357: W/System.err(292):  at java.lang.reflect.Method.invoke(Method.java:521)
05-17 13:39:25.357: W/System.err(292):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-17 13:39:25.357: W/System.err(292):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-17 13:39:25.357: W/System.err(292):  at dalvik.system.NativeStart.main(Native Method)

我不完全理解这个错误,但由于它是权限,我得出了一些结论:

  • 无法连接到mysql数据库
  • 与 IP 地址和我的服务器有关吗?

最佳答案

将此添加到您的 list 并检查。

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

关于java - Android Java 应用程序 - java.net.SocketException : Permission denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610974/

相关文章:

java - 是什么导致了我的 java.net.SocketException : Connection reset?

java - Java 中的连接重置

java - 我是否需要一个字符集过滤器来仅将某些页面显示为带有 tomcat 的 UTF8

java - 重定向到 JSP 而不是来自 REST 调用的 JSON 响应

java - 如何使用 Tiles 进行类似 Facelets 的模板合成?

java - Robospice 异常从未在 RequestListener 中捕获

java - 尝试使用 Fitnesse 和 DbFit 以及 SQL Server 时出现 SQLServerException

android - setText()不适用于linearLayout中动态添加的按钮

android - 如何将通知从一个安卓设备发送到另一个?

c# - 创建 CreateBatchAsync 并在 VPN 中连接时出现 Azure EventHub 套接字异常 10054