Android的,如何发送POST请求 [英] Android, how to send POST request

查看:220
本文介绍了Android的,如何发送POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从我的Andr​​oid应用程序发送POST请求到服务器。
我发现有关如何发送一些例子发表但我有一些错误与我的code和这里是code:

I can't send post request to the server from my Android app. I have found some examples about how to send POST but I have some error with my code and here is the code:

public class MainActivity extends Activity 
{
private WebView wv; //Internet
private EditText email1; //Edit's
private EditText email2; //Edit's
private Button btn_get_access; //Get Access
private String post_url = "http://rasnacis.lv/vova.php";

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

    wv = (WebView) findViewById(R.id.webView1);
    email1 = (EditText) findViewById(R.id.txt_email_1);
    email2 = (EditText) findViewById(R.id.txt_email_2);
    btn_get_access = (Button) findViewById(R.id.btn_get_access);

    WebSettings webSettings = wv.getSettings();
    webSettings.setSaveFormData(true);

    //BUTTON
    OnClickListener ocl_btn_get_access = new OnClickListener()
    {

        public void onClick(View v) 
        {

            String givenEmail1 = email1.getEditableText().toString();
            String givenEmail2 = email2.getEditableText().toString();

            //SENDING POST
            if (givenEmail1.length() > 0 && givenEmail2.length() > 0)
            {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(post_url);

                try
                {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("email1", "email2"));
                    nameValuePairs.add(new BasicNameValuePair("email1", "slgjlskjgsg"));
                    nameValuePairs.add(new BasicNameValuePair("email2", "xkjfhgkdjfhgkdjfg"));

                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    httpClient.execute(httpPost);
                }
                catch (ClientProtocolException e) 
                {
                    System.out.println("First Exception caz of HttpResponese :" + e);
                    e.printStackTrace();
                }
                catch (IOException e) 
                {
                    System.out.println("Second Exception caz of HttpResponse :" + e);
                    e.printStackTrace();
                }
            }
            else
            {
                Toast.makeText(getBaseContext(), "All fields are required!", Toast.LENGTH_SHORT).show();
            }

            //sending GET
            //wv.loadUrl("http://rasnacis.lv/vova.php?email1=" + email1.getText() + "&email2=" + email2.getText());
        }
    };
    btn_get_access.setOnClickListener(ocl_btn_get_access);
}

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

}

所以有人帮助我呢?我只是开始Android开发,不知道很多窍门什么困难......

so can somebody help me with it? i am just started Android development and don't know many tricks or something difficult...

推荐答案

这是在 doPostRequest()方式在应用程序中使用,

This is the doPostRequest() method to use in your application,

这是对您有用和完美的工作的在我的code ...

that is useful for you and perfectly working in my code...

private void doPostRequest(){

    String urlString = "http://rasnacis.lv/vova.php";
    try
    {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(urlString);

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("email1_tag", new StringBody("email1_put_here"));
        reqEntity.addPart("email2_tag", new StringBody("email2_put)here"));
        reqEntity.addPart("email3_tag", new StringBody("email3_put_here"));
        post.setEntity(reqEntity);
        HttpResponse response = client.execute(post);
        resEntity = response.getEntity();
        final String response_str = EntityUtils.toString(resEntity);
        if (resEntity != null) {
            Log.i("RESPONSE",response_str);
            runOnUiThread(new Runnable(){
                public void run() {
                    try {
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    catch (Exception ex){
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
}

如果您实现这个code您要求两个文件:

If you Implement this code you required two library files:

http://repo1.maven.org/maven2/org/apache/httpcomponents/httpmime/4.0.1/httpmime-4.0.1.jar

http://repo1.maven.org/maven2/org/apache/james/apache-mime4j/0.6/apache-mime4j-0.6.jar

这篇关于Android的,如何发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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