Android系统。使用get请求注册

时间:2014-06-04 06:28:27

标签: android

我正在尝试使用get请求在Android上实现用户注册。它需要一个电子邮件和密码。所以它不起作用,没有任何东西被发送到服务器。感谢那些试图帮助我的人。

RegWindow.java

package com.example.sadaka;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

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

public class RegWindow extends Activity {
    EditText name;
    EditText lastname;
    EditText address;
    EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_window);

        Button r_confirm,r_login,r_skip;
        name=(EditText)findViewById(R.id.name);
        lastname=(EditText)findViewById(R.id.lastname);
        address=(EditText)findViewById(R.id.address);
        password=(EditText)findViewById(R.id.r_password);
        r_confirm=(Button)findViewById(R.id.r_confirm);
        r_login=(Button)findViewById(R.id.r_login);
        r_skip=(Button)findViewById(R.id.r_skip);

        r_confirm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                try{
                    String loginValue    = URLEncoder.encode(name.getText().toString(), "UTF-8");
                    String fnameValue  = URLEncoder.encode(lastname.getText().toString(), "UTF-8");
                    String emailValue   = URLEncoder.encode(address.getText().toString(), "UTF-8");
                    String passValue    = URLEncoder.encode(password.getText().toString(), "UTF-8");
                    HttpClient Client = new DefaultHttpClient();
                    String URL="http://sadakafund.com/api/add_user.php?name="+loginValue+"&surname="+fnameValue+"&email="+emailValue+"&password="+passValue;
                   try{
                    String SetServerString = "";
                    HttpGet httpget = new HttpGet(URL);
                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    SetServerString = Client.execute(httpget, responseHandler);

                    }catch(Exception ex){
                        ex.printStackTrace();
                        name.setText("error!!! ");
                    }
                }
                catch(UnsupportedEncodingException ex){
                    name.setText("error");
                }
                }

        });
        r_login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent("com.example.sadaka.Splash");
                startActivity(intent);
            }
        });
        r_skip.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent("com.example.sadaka.MAINACTIVITY");
                startActivity(intent);
            }
        });
    }

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

}

reg_window.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/splash" >

    <EditText 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Имя"/>

    <EditText
        android:id="@+id/lastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Фамилия"/>

    <EditText
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="e-mail"/>

    <EditText
        android:id="@+id/r_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="password"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/r_confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="register"/>

    <Button 
        android:id="@+id/r_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="login"/>

    <Button
        android:id="@+id/r_skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="skip"/>"

</LinearLayout>

logcat的

    06-04 10:11:52.238: W/EGL_genymotion(3436): eglSurfaceAttrib not implemented
06-04 10:11:58.190: W/EGL_genymotion(3436): eglSurfaceAttrib not implemented
06-04 10:12:07.390: W/EGL_genymotion(3436): eglSurfaceAttrib not implemented
06-04 10:12:11.470: W/System.err(3436): android.os.NetworkOnMainThreadException
06-04 10:12:11.470: W/System.err(3436):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
06-04 10:12:11.470: W/System.err(3436):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-04 10:12:11.470: W/System.err(3436):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-04 10:12:11.470: W/System.err(3436):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-04 10:12:11.470: W/System.err(3436):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
06-04 10:12:11.470: W/System.err(3436):     at com.example.sadaka.RegWindow$1.onClick(RegWindow.java:60)
06-04 10:12:11.470: W/System.err(3436):     at android.view.View.performClick(View.java:4240)
06-04 10:12:11.470: W/System.err(3436):     at android.view.View$PerformClick.run(View.java:17721)
06-04 10:12:11.474: W/System.err(3436):     at android.os.Handler.handleCallback(Handler.java:730)
06-04 10:12:11.474: W/System.err(3436):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 10:12:11.474: W/System.err(3436):     at android.os.Looper.loop(Looper.java:137)
06-04 10:12:11.474: W/System.err(3436):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-04 10:12:11.474: W/System.err(3436):     at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:12:11.474: W/System.err(3436):     at java.lang.reflect.Method.invoke(Method.java:525)
06-04 10:12:11.474: W/System.err(3436):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-04 10:12:11.474: W/System.err(3436):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-04 10:12:11.474: W/System.err(3436):     at dalvik.system.NativeStart.main(Native Method)
06-04 10:14:59.498: D/dalvikvm(3513): GC_FOR_ALLOC freed 147K, 9% free 2715K/2972K, paused 2ms, total 2ms
06-04 10:14:59.498: I/dalvikvm-heap(3513): Grow heap (frag case) to 3.610MB for 901180-byte allocation
06-04 10:14:59.506: D/dalvikvm(3513): GC_FOR_ALLOC freed 2K, 7% free 3593K/3856K, paused 7ms, total 7ms
06-04 10:14:59.518: D/dalvikvm(3513): GC_FOR_ALLOC freed <1K, 7% free 3593K/3856K, paused 2ms, total 2ms
06-04 10:14:59.522: I/dalvikvm-heap(3513): Grow heap (frag case) to 7.045MB for 3604684-byte allocation
06-04 10:14:59.526: D/dalvikvm(3513): GC_FOR_ALLOC freed <1K, 4% free 7113K/7380K, paused 4ms, total 4ms
06-04 10:14:59.622: D/libEGL(3513): loaded /system/lib/egl/libEGL_genymotion.so
06-04 10:14:59.622: D/(3513): HostConnection::get() New Host Connection established 0xb7f81060, tid 3513
06-04 10:14:59.630: D/libEGL(3513): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
06-04 10:14:59.630: D/libEGL(3513): loaded /system/lib/egl/libGLESv2_genymotion.so
06-04 10:14:59.674: W/EGL_genymotion(3513): eglSurfaceAttrib not implemented
06-04 10:14:59.674: E/OpenGLRenderer(3513): Getting MAX_TEXTURE_SIZE from GradienCache
06-04 10:14:59.686: E/OpenGLRenderer(3513): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
06-04 10:14:59.686: D/OpenGLRenderer(3513): Enabling debug mode 0
06-04 10:15:02.086: W/EGL_genymotion(3513): eglSurfaceAttrib not implemented
06-04 10:15:12.858: W/EGL_genymotion(3513): eglSurfaceAttrib not implemented
06-04 10:15:17.398: W/EGL_genymotion(3513): eglSurfaceAttrib not implemented
06-04 10:15:21.026: W/EGL_genymotion(3513): eglSurfaceAttrib not implemented
06-04 10:15:29.690: W/EGL_genymotion(3513): eglSurfaceAttrib not implemented
06-04 10:15:33.630: W/System.err(3513): android.os.NetworkOnMainThreadException
06-04 10:15:33.638: W/System.err(3513):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
06-04 10:15:33.638: W/System.err(3513):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-04 10:15:33.638: W/System.err(3513):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-04 10:15:33.638: W/System.err(3513):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
06-04 10:15:33.638: W/System.err(3513):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
06-04 10:15:33.642: W/System.err(3513):     at com.example.sadaka.RegWindow$1.onClick(RegWindow.java:60)
06-04 10:15:33.642: W/System.err(3513):     at android.view.View.performClick(View.java:4240)
06-04 10:15:33.642: W/System.err(3513):     at android.view.View$PerformClick.run(View.java:17721)
06-04 10:15:33.642: W/System.err(3513):     at android.os.Handler.handleCallback(Handler.java:730)
06-04 10:15:33.642: W/System.err(3513):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 10:15:33.642: W/System.err(3513):     at android.os.Looper.loop(Looper.java:137)
06-04 10:15:33.642: W/System.err(3513):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-04 10:15:33.642: W/System.err(3513):     at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:15:33.646: W/System.err(3513):     at java.lang.reflect.Method.invoke(Method.java:525)
06-04 10:15:33.646: W/System.err(3513):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-04 10:15:33.646: W/System.err(3513):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-04 10:15:33.646: W/System.err(3513):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

问题是您正在尝试在主线程中进行联网,从而捕获NetworkOnMainThreadException。在android中进行网络连接时,需要在单独的线程中进行。

<强>溶液

Asynctask是最好的解决方案。您可以click here获取有关Asynctask的更多信息。   将所有网络内容放在AsyncTask的doInBackground()方法中,因为它在一个单独的线程上运行。

答案 1 :(得分:0)

您可以将onClick代码移至自定义异步类。检查下面的示例

   class CustomAsync extends AsyncTask<Void, Void, Void>{

        protected doBackground(String... URL){
                        HttpClient Client = new DefaultHttpClient();
                       try{
                        String SetServerString = "";
                        HttpGet httpget = new HttpGet(URL[0]);
                        ResponseHandler<String> responseHandler = new BasicResponseHandler();
                        SetServerString = Client.execute(httpget, responseHandler);

         }catch(Exception e){}
        }

        protected void onPostExecute(Long result) {
        }

使用新的CustomAsync()调用。执行(&#34; http://sadakafund.com/api/add_user.php?name=&#34; + loginValue +&#34;&amp; surname =&#34; + fnameValue +&#34;&amp; email =&#34 + + emailValue&#34;&安培;密码=&#34 + passValue&#34);