简体   繁体   中英

error java.net.socketException: permission denied mysql android

can someone help me plsss, the error message comes in the textbox on the login form, the error is when i submit my information on the first form it won't go to database instead showing error "error java.net.socketException: permission denied" here's my code: 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> 

You have forgotten the internet permission in your manifest file.

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

After that you will get an networkonmainthreadexception because you are doing web request in the main thread.
Use another AsyncTask or another thread for that.

You have to add the internet permission to you manifiest file:

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

And also Android wont let you do http connections in the main thread, for more info look up:

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

You did forget too add

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

in your manifest.xml.

But after that you will receive a networkmainthreadexception, to avoid this you need to use AsyncTask or an extra thread . If you get problems to use this things you read this post which may helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM