簡體   English   中英

錯誤java.net.socketException:權限被拒絕的mysql android

[英]error java.net.socketException: permission denied mysql android

有人可以幫助我,錯誤消息出現在登錄表單上的文本框中,錯誤是當我在第一個表單上提交我的信息時,它不會進入數據庫,而是顯示錯誤“錯誤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> 

您已忘記清單文件中的Internet權限。

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

之后,您將獲得networkonmainthreadexception,因為您正在主線程中執行Web請求。
為此使用另一個AsyncTask或另一個線程。

您必須將Internet權限添加到您的manifiedt文件中:

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

Android也不允許您在主線程中進行HTTP連接,有關更多信息,請查詢:

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

你忘了也加

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

在manifest.xml中。

但是之后,您將收到networkmainthreadexception,為避免這種情況,您需要使用AsyncTask或額外的線程 如果您在使用這些東西時遇到問題,請閱讀這篇文章 ,這可能會對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM