Android的登录方法后 [英] android login post method

查看:502
本文介绍了Android的登录方法后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始与Android发展,我要创建一个登录表单至极点击一个按钮,它会发出这样的:的 http://www.mypage.com/?U=USUARI&K=PASSWORD
搜索在这里,我成立了维卡斯Patidar上传NAD我trye​​d把它修改我的需求的方法。但是我觉得有什么不对,因为它不工作。
你能告诉我在哪里,我弄错吗?

Hi I'm starting with android developing, and I have to create a login form wich clicking a button, it'll send this: http://www.mypage.com/?U=USUARI&K=PASSWORD. Searching in here I've founded a method that Vikas Patidar uploaded nad I've tryed to modify it to my needs. But I think there's something wrong because it doesn't works. Can you tell me where I am mistaking please?

这是code
`

This is the code `

public class HttpLogin extends Activity { /** Called when the activity is first created. */ private Button login; private EditText U, K;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    login = (Button) findViewById(R.id.login);
    U = (EditText) findViewById(R.id.U);
    K = (EditText) findViewById(R.id.K);

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String   mUsername = U.getText().toString();
            String  mPassword = K.getText().toString();

            tryLogin(mUsername, mPassword);
        }
    });
}

protected void tryLogin(String mUsername, String mPassword)
{           
    HttpURLConnection connection;
   OutputStreamWriter request = null;

        URL url = null;   
        String response = null;         
        String parameters = "U="+mUsername+"&K="+mPassword;   

        try
        {
            url = new URL("http://http://www.mypage.com/content/frmLogin.aspx");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");    

            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();            
            String line = "";               
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            // Response from server after login process will be stored in response variable.                
            response = sb.toString();
            // You can perform UI operations here
            Toast.makeText(this,"Message from Server: \n"+ response, 0).show();             
            isr.close();
            reader.close();

        }
        catch(IOException e)
        {
            // Error
        }
}
´

和XML是这样的:

and the xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  >
<EditText 
android:hint="Username" 
android:id="@+id/U" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">
</EditText>

<EditText 
android:hint="Password" 
android:id="@+id/K" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:inputType="textPassword">
</EditText>

<Button 
android:text="Iniciar Sessió" 
android:id="@+id/login" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">
</Button>
</LinearLayout>


现在我与HTTP GET方法尝试,我得到这个


Now I'm trying with HTTP GET method and I got this

packge com.android.v3;

import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class v3act extends Activity {
    TextView Tname,Tpass;
    EditText Ename,Epass;
    Button btnCreate;
     String n=null;
     String contentOfMyInputStream1;
     String output = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnCreate = (Button) findViewById(R.id.btnGen);
        Tname = (TextView) findViewById(R.id.txtName);
        Ename = (EditText) findViewById(R.id.editName);
        Tpass = (TextView) findViewById(R.id.txtPass);
        Epass = (EditText) findViewById(R.id.editPass);




        btnCreate.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            Tname.setText("");
            // Thread thread = new Thread();
             String st1;

             st1=Ename.getText().toString();
            //thread.start();

             Tpass.setText("");
             // Thread thread = new Thread();
              String st2;

              st2=Epass.getText().toString();
             //thread.start();

            try {
                 output ="http://www.mypage.com/?U="+st1+"K="+st2;
                downloadUrl(output);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (output != null) 
            {
                Tname.setText(output);
            }


            }
            });


    }

    public String downloadUrl(String url) throws  IOException{
        HttpClient httpclient = new DefaultHttpClient();  
        HttpRequestBase httpRequest = null;
        HttpResponse httpResponse = null;
        InputStream inputStream = null;
        String response = "";
        StringBuffer buffer = new StringBuffer();

        httpRequest = new HttpGet(url); 

        httpResponse = httpclient.execute(httpRequest);
        inputStream = httpResponse.getEntity().getContent();
        int contentLength = (int) httpResponse.getEntity().getContentLength();
        if (contentLength < 0){
           // Log.e(TAG, "The HTTP response is too long.");
        }
        byte[] data = "8" byte[256];
        int len = 0;
        while (-1 != (len = inputStream.read(data)) )
        {
            buffer.append(new String(data, 0, len));
        }

        inputStream.close();

        response = buffer.toString();

        return response;

    }


}

但我上线的一些错误,其中有云:

but I have some mistake on the line where goes:

byte[] data = "8" byte[256];

它说:令牌字节语法错误,删除此令牌
如果我删除,我在reacton得到更多的错误。

It says: 'Syntax error on token "byte", delete this token' And if I delete that, I get more errors in reacton.

我应该怎么办?

推荐答案

删除

The conn.setRequestMethod("POST");

然后 conn.setDoOutput(真); 已表示您使用POST沟通。

and then the conn.setDoOutput(true); already means you are using POST communication.

下一步。

        request.write(parameters);
        request.flush();
        request.close();

将其更改为

        request.write(parameters.getBytes());
        request.flush();
        //request.close();//it is closing prematurly so you will definitely not get any response at all... 

尝试这些了,让我知道,如果它原来好...

try these out and let me know if it turns out okey...

如果你想看看尝试这个例子这看起来几乎相似你的情况...

if you want to check out try this example which looks almost similar to your conditions...

可能是,这将有助于tooo ....回应。

Might be that will help tooo.... respond.

这篇关于Android的登录方法后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆