Android:点击网站上的按钮

时间:2013-03-12 20:49:41

标签: android post

我有一个问题,我在Android设备和某个网站上有应用程序,在按钮上处理POST检查,所以我发送POST请求到这个网站,有正确的数据,并且不能通过传递这个POST以图解方式点击检查服务器端,我需要用我的应用程序或服务器端代码做什么?

这是我发送POST的代码:

/****** SEND POST REQUEST ******/
private void sendPostRequest_cabinet(String clientID) 
{
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> 
    {

        @Override
        protected String doInBackground(String... params) 
        {

            // insert data into massive
            String paramClientID = params[0];

            HttpClient httpClient = new DefaultHttpClient();
            //httpPost = new HttpPost(post_url + /*language from dropdown + */ "/en/login/");


            if (LANGUAGE != null)
            {           
                if (LANGUAGE.equals("English"))
                {
                    httpPost = new HttpPost(link.getEng());
                    Toast.makeText(getApplicationContext(), (CharSequence) httpPost, Toast.LENGTH_LONG).show();
                }
                else if (LANGUAGE.equals("Russian"))
                {
                    httpPost = new HttpPost(link.getRussian());
                }
                else if (LANGUAGE.equals("Dutch(German)"))
                {
                    httpPost = new HttpPost(link.getDeutch());
                }
                else if (LANGUAGE.equals("French"))
                {
                    httpPost = new HttpPost(link.getFrance());
                }
                else if (LANGUAGE.equals("Italian"))
                {
                    httpPost = new HttpPost(link.getItalian());
                }
                else if (LANGUAGE.equals("Spanish"))
                {
                    httpPost = new HttpPost(link.getSpanish());
                }
            }

            // send parameters with values
            BasicNameValuePair email1BasicNameValuePair = new BasicNameValuePair("client_id", paramClientID);

            // insert parameters into massive
            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
            nameValuePairList.add(email1BasicNameValuePair);

            try 
            {
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
                httpPost.setEntity(urlEncodedFormEntity);

                try 
                {
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    InputStream inputStream = httpResponse.getEntity().getContent();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    BufferedReader buffereReader = new BufferedReader(inputStreamReader);
                    StringBuilder stringBuilder = new StringBuilder();

                    String bufferedStrChunk = null;

                    while ((bufferedStrChunk = buffereReader.readLine()) != null) 
                    {
                        stringBuilder.append(bufferedStrChunk);
                    }

                    return stringBuilder.toString();
                }
                catch (ClientProtocolException cpe) 
                {
                    System.out.println("First Exception of HttpResponse: " + cpe);
                    cpe.printStackTrace();
                } 
                catch (IOException ioe) 
                {
                    System.out.println("Second Exception of HttpResponse: " + ioe);
                    ioe.printStackTrace();
                }
            } 
            catch (UnsupportedEncodingException uee) 
            {
                System.out.println("An Exception given  because of UrlEncodedFormEntity argument : " + uee);
                uee.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result)
        {
            // Toast.makeText(getApplicationContext(),
            // "POST request has been send successfully",
            // Toast.LENGTH_LONG).show();

            super.onPostExecute(result);

            //Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

            //go to the browser with lang redirect              
            if (LANGUAGE != null)
            {           
                if (LANGUAGE.equals("English"))
                {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(link.getEng()));
                    startActivity(i);
                }
                else if (LANGUAGE.equals("Russian"))
                {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(link.getRussian()));
                    startActivity(i);
                }
                else if (LANGUAGE.equals("Dutch(German)"))
                {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(link.getDeutch()));
                    startActivity(i);
                }
                else if (LANGUAGE.equals("French"))
                {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(link.getFrance()));
                    startActivity(i);
                }
                else if (LANGUAGE.equals("Italian"))
                {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(link.getItalian()));
                    startActivity(i);
                }
                else if (LANGUAGE.equals("Spanish"))
                {
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(link.getSpanish()));
                    startActivity(i);
                }
            }
        }
    }
    /***** second check of Internet connection *****/
    if (!isOnline()) 
    {
        showNoConnectionDialog(this);
        // Toast.makeText(getApplicationContext(),
        // "Internet connection is disabled!", Toast.LENGTH_LONG).show();
    } 
    else 
    {
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(clientID);
    }
}

UPD 这是我如何发送数据并从以前的活动中获取数据:

//go to the ThirdScreen
Intent intent = new Intent(SecondScreen.this, ThirdScreen.class);
intent.putExtra("CLIENT_ID", stringClientID);
intent.putExtra("PASSWORD", getUserPWD);
intent.putExtra("LANGUAGE", language);
startActivity(intent);
finish();

Intent intent = getIntent();
CLIENT_ID = intent.getExtras().getString("CLIENT_ID");

UPD2 也许我没有清楚地解释我需要什么,所以我会尝试解释...... 我有网站,到这个网站我需要通过POST发送一些数据,这是什么意思?我启动我的Android应用程序,用数据填写一些文本框,然后单击按钮,单击此按钮我需要打开WebBrowser并将数据发送到此站点......

1 个答案:

答案 0 :(得分:0)

我通过在其中创建包含WebView组件的新活动并检索所需网站来解决我的问题,因此主持人可以关闭此问题。