java - 将表单数据发送到服务器 Api (Android)

标签 java android api

我需要向服务器 API 提交我的注册表单详细信息。我尝试了一种方法,通过单击按钮调用函数。但是服务器上没有发布任何内容,我也没有收到任何异常。请帮助我。 提前致谢。 下面是代码。

我已经这样调用了这个函数

 button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try{

                // CALL GetText method to make post method call
                GetText();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();;
                Toast.makeText(getActivity(),"URL Exception", Toast.LENGTH_LONG).show();
            }

下面给出的是函数

  public String GetText()  throws UnsupportedEncodingException
{
    HttpURLConnection connection = null;
    // Get user defined values
    Name = full_name.getText().toString();
    Phonenumber = phone.getText().toString();
    Email=email.getText().toString();
    Password=password.getText().toString();
    House = house.getText().toString();
    Street = street.getText().toString();
    Landmark = landmark.getText().toString();
    // Create data variable for sent values to server
    String data = URLEncoder.encode("name", "UTF-8")
            + "=" + URLEncoder.encode(Name, "UTF-8");
    data += "&" + URLEncoder.encode("email", "UTF-8") + "="
            + URLEncoder.encode(Email, "UTF-8");
    data += "&" + URLEncoder.encode("password", "UTF-8") + "="
            + URLEncoder.encode(Password, "UTF-8");
    data += "&" + URLEncoder.encode("phone", "UTF-8")
            + "=" + URLEncoder.encode(Phonenumber, "UTF-8");
    data += "&" + URLEncoder.encode("house", "UTF-8")
        + "=" + URLEncoder.encode(House, "UTF-8");
    data += "&" + URLEncoder.encode("street", "UTF-8")
            + "=" + URLEncoder.encode(Street, "UTF-8");
    data += "&" + URLEncoder.encode("areaname", "UTF-8")
            + "=" + URLEncoder.encode(Area, "UTF-8");
    data += "&" + URLEncoder.encode("landmark", "UTF-8")
            + "=" + URLEncoder.encode(Landmark, "UTF-8");

    // Send data
    try
    {

        // Defined URL  where to send data
        URL url = new URL("http://application.easypani.com/app/customer/register");
        // Send POST data request
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", "" +
                Integer.toString(data.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        //send request
        DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream ());
        wr.writeBytes(data);
        wr.flush();
        wr.close();

        // Get the server response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();

    } catch (Exception e) {

        e.printStackTrace();
        return null;

    } finally {

        if(connection != null) {
            connection.disconnect();
        }
    }
}

最佳答案

用这个代替

private class PostData extends AsyncTask < String, Void, Void > {
    protected Void doInBackground(String...params) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://application.easypani.com/app/customer/register");

        try {
            // Add your data
            String Name = params[0];
            String Phonenumber = params[1];
            String Email = params[2];
            String Password = params[3];
            String House = params[4];
            String Street = params[5];
            String Landmark = params[6];

            List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > ();
            nameValuePairs.add(new BasicNameValuePair("name", Name));
            nameValuePairs.add(new BasicNameValuePair("email", Email));............................................................
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }
}

你这样使用它

new PostData().execute(Name, PhoneNumber..and so on);

关于java - 将表单数据发送到服务器 Api (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32984723/

相关文章:

java - Hibernate @AttributeOverrides 多个属性而不显式命名每个属性

java - 如何比较 Android Firebase 中不同引用的两个子节点?

php - 在 firebase 上为参数创建自定义数据以调用推送通知

amazon-web-services - 使用 API key 或授权方授权 AWS API Gateway

javascript - Etsy API 列表(按类别)

java - AspectJ:向类添加静态初始值设定项

java - Google ANR 崩溃报告,您能联系到用户吗?

java - 如何找到多位数字中的最高位?

java - 启动/停止媒体播放器 android studio

api - 远程控制 API 或如何启动?