错误java.net.socketException:权限被拒绝mysql android

时间:2014-04-02 08:14:01

标签: java android mysql xampp database-connection

有人可以帮助我plsss,错误信息出现在登录表单的文本框中,错误是当我在第一个表单上提交我的信息时它不会转到数据库而是显示错误"错误java.net.socketException:权限被拒绝"这是我的代码: tambah_user.java

package com.wilis.hellomysql;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class tambah_user extends Activity {

   EditText un,pw,rpw,nl,al,nt,nh;
   RadioGroup jk;
   TextView error;
   Button simpan,keluar;


    /** Called when the activity is first created. */

   @Override


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tambah_user);

        un=(EditText)findViewById(R.id.et_un);
        pw=(EditText)findViewById(R.id.et_pw);
        rpw=(EditText)findViewById(R.id.et_rpw);
        nl=(EditText) findViewById(R.id.et_nama);
        jk=(RadioGroup) findViewById(R.id.jekel);
        al=(EditText) findViewById(R.id.et_alamat);
        nt=(EditText) findViewById(R.id.et_notel);
        nh=(EditText) findViewById(R.id.et_nohp);


        simpan=(Button)findViewById(R.id.btn_simpan);
        keluar=(Button)findViewById(R.id.btn_keluar);
        error=(TextView)findViewById(R.id.error);
        simpan.setOnClickListener(new View.OnClickListener() {

         @Override

         public void onClick(View v) {

            // TODO Auto-generated method stub

            //atur variabel utk menampung pilihan jenis kelamin
             String type=null;
             switch (jk.getCheckedRadioButtonId()) {
             case R.id.pria:
             type="Pria";
             break;
             case R.id.perempuan:
             type="Perempuan";
             break;
             }


            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            postParameters.add(new BasicNameValuePair("repassword", rpw.getText().toString()));
            postParameters.add(new BasicNameValuePair("nama", nl.getText().toString()));
            postParameters.add(new BasicNameValuePair("jekel", type));
            postParameters.add(new BasicNameValuePair("alamat", al.getText().toString()));
            postParameters.add(new BasicNameValuePair("nomor_tlp", nt.getText().toString()));
            postParameters.add(new BasicNameValuePair("nomor_hp", nh.getText().toString()));

/*            String valid = "1";*/      

            String response = null;

            try {

               response = CustomHttpClient.executeHttpPost("http://10.0.2.2/appmysql/simpan.php", postParameters);

               String res = response.toString();

               res = res.trim();

               res = res.replaceAll("\\s+","");

               error.setText(res);

               if (res.equals("1")) error.setText("Data Tersimpan");

               else error.setText("Data Tersimpan Ke server");

            }

            catch (Exception e) {

               un.setText(e.toString());
            }
         } 
      });
    }

    public void keluar (View theButton)
    {
    }
}

CustomHttpClient.java

package com.wilis.hellomysql;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class CustomHttpClient {
    public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds

    private static HttpClient mHttpClient;

    private static HttpClient getHttpClient() {
        if (mHttpClient == null) {
            mHttpClient = new DefaultHttpClient();
            final HttpParams params = mHttpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
            ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
        }
        return mHttpClient;
    }

    public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = getHttpClient();
            HttpPost request = new HttpPost(url);
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            String result = sb.toString();
            return result;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String executeHttpGet(String url) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = getHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            String result = sb.toString();
            return result;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

tambah_user.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff00ffff"
    >

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
android:text="Silakan Masukkan Data Pengguna"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#ff0000ff"
/>

<TableRow android:baselineAligned="true" android:layout_width="match_parent">
<TextView
android:text="Username:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>


<EditText android:id="@+id/et_un" 
android:maxWidth="140sp"
android:layout_height="wrap_content" 
android:layout_width="match_parent"
android:layout_gravity="center_vertical" >
</EditText>
</TableRow>

<TableRow>
<TextView
android:text="Password:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>

<EditText android:id="@+id/et_pw" 
android:layout_height="wrap_content" 
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:inputType="textPassword">
</EditText>
</TableRow>


<TableRow>
<TextView
android:text="retype-Password:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>


<EditText android:layout_height="wrap_content" 
android:id="@+id/et_rpw" 
android:layout_width="match_parent"
android:inputType="textPassword">
</EditText>
</TableRow>



<TableRow>
<TextView
android:text="Nama Lengkap:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>


<EditText android:layout_height="wrap_content" 
android:id="@+id/et_nama" 
android:layout_width="match_parent">
</EditText>
</TableRow>

<TableRow>
<TextView android:text="Jekel:" 
          android:textColor="#ff0000ff"/>

<RadioGroup android:id="@+id/jekel">
<RadioButton android:id="@+id/pria"
             android:text="Pria"
/>
<RadioButton android:id="@+id/perempuan"
             android:text="Perempuan"
/>
</RadioGroup>
</TableRow>


<TableRow>
<TextView
android:text="Alamat:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>


<EditText android:layout_height="wrap_content" 
android:id="@+id/et_alamat" 
android:layout_width="match_parent">
</EditText>
</TableRow>

<TableRow>
<TextView
android:text="Nomor Tlp:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>


<EditText android:layout_height="wrap_content" 
android:id="@+id/et_notel" 
android:layout_width="match_parent">
</EditText>
</TableRow>

<TableRow>
<TextView
android:text="Nomor HP:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>


<EditText android:layout_height="wrap_content" 
android:id="@+id/et_nohp" 
android:layout_width="match_parent">
</EditText>
</TableRow>


<TableRow >
<Button android:text="S I M P A N" 
android:id="@+id/btn_simpan" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>

<Button android:text="K E L U A R" 
android:id="@+id/btn_keluar" 
android:onClick="keluar"
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</Button>

</TableRow>

<TextView
android:text=""
android:id="@+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000ff"
/>

</TableLayout>
</ScrollView>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.wilis.hellomysql"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".tambah_user"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CustomHttpClient" android:label="@string/app_name">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest> 

3 个答案:

答案 0 :(得分:0)

您忘记了清单文件中的互联网权限。

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

之后,您将获得networkonmainthreadexception,因为您正在主线程中执行Web请求 使用另一个AsyncTask或其他线程。

答案 1 :(得分:0)

您必须为清单文件添加互联网权限:

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

Android也不会让你在主线程中进行http连接,以获取更多信息:

http://developer.android.com/reference/android/os/AsyncTask.html

答案 2 :(得分:0)

你确实忘记了添加

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

但之后您将收到networkmainthreadexception,为避免这种情况,您需要使用AsyncTask或额外的thread。如果您在使用这些内容时遇到问题,请阅读post,这可能会对您有所帮助。