2014-04-02 44 views
0

有人可以幫我解決問題,錯誤信息出現在登錄表單上的文本框中,錯誤是當我在第一個表單上提交我的信息時它不會去數據庫,而不是顯示錯誤 「錯誤java.net.SocketException異常:權限被拒絕」 這裏是我的代碼: tambah_user.javaerror java.net.socketException:permission denied mysql android

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> 

回答

0

你忘清單檔案中的Internet權限。

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

之後,您將得到一個networkonmainthreadexception,因爲您在主線程中執行Web請求。
使用另一個AsyncTask或其他線程。

0

你沒有忘記過添加

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

在你的manifest.xml中。

但之後,您將收到networkmainthreadexception,爲了避免這種情況,您需要使用AsyncTask或額外的thread。如果您在使用這些東西時遇到問題,請閱讀post,這可能會對您有所幫助。