如何在Android中执行HTTP发布?

如何在Android中执行HTTP发布?,第1张

概述我是Android应用程序开发的新手,我需要的是我有两个文本框用户名和密码,它会发布到服务器并使用php页面检查它,如果登录成功则转到下一个屏幕,否则显示一个msg框显示登录错误我该怎么办?publicvoidpostData(){//CreateanewHttpClientandPostHeaderHttpClient

我是Android应用程序开发的新手,我需要的是我有两个文本框用户名和密码,它会发布到服务器并使用PHP页面检查它,如果登录成功则转到下一个屏幕,否则显示一个msg框显示登录错误我该怎么办?

public voID postData() {    // Create a new httpClIEnt and Post header    httpClIEnt httpclIEnt = new DefaulthttpClIEnt();    httpPost httppost = new httpPost("http://Google.com");    EditText tw =(EditText) findVIEwByID(R.ID.EditText01);    try {        // Add your data        List<nameValuePair> nameValuePairs = new ArrayList<nameValuePair>(2);        nameValuePairs.add(new BasicnameValuePair("ID", "12345"));        nameValuePairs.add(new BasicnameValuePair("stringdata", "AndDev is Cool!"));        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        // Execute http Post Request        httpResponse response = httpclIEnt.execute(httppost);        int status = response.getStatusline().getStatusCode();        tw.setText(status);    } catch (ClIEntProtocolException e) {        tw.setText(e.toString());    } catch (IOException e) {        tw.setText(e.toString());    }} 

解决方法:

使用这个类:

import java.io.BufferedReader;import java.io.IOException;import java.io.inputStreamReader;import java.io.OutputStreamWriter;import java.net.httpURLConnection;import java.net.URL;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.Toast;public class httpLogin extends Activity {    /** Called when the activity is first created. */    private button login;    private EditText username, password;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        login = (button) findVIEwByID(R.ID.login);        username = (EditText) findVIEwByID(R.ID.username);        password = (EditText) findVIEwByID(R.ID.password);        login.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                String   mUsername = username.getText().toString();                String  mPassword = password.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 = "username="+mUsername+"&password="+mPassword;               try            {                url = new URL("your login URL");                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            }    }}

main.xml将是这样的:

<?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/username" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content"></EditText><EditText androID:hint="Password" androID:ID="@+ID/password" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:inputType="textPassword"></EditText><button androID:text="Sign In" androID:ID="@+ID/login" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content"></button></linearLayout>
总结

以上是内存溢出为你收集整理的如何在Android中执行HTTP发布?全部内容,希望文章能够帮你解决如何在Android中执行HTTP发布?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104038.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存