2012-08-26 140 views
2

我知道這已經被問過,但我掙扎..我幾乎不知道任何Java,但想要一個應用程序,所以我可以做我的POST請求,而無需打開我的網站。在Android應用上發出POST請求

所以我有這個佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:ems="10" 
     android:inputType="textPersonName" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Name:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:ems="10" 
     android:inputType="textPostalAddress" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignTop="@+id/editText2" 
     android:text="Address:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/editText2" 
     android:ems="10" 
     android:inputType="phone" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/editText2" 
     android:text="Phone:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/editText4" 
     android:layout_width="wrap_content" 
     android:layout_height="200dp" 
     android:layout_alignLeft="@+id/editText3" 
     android:layout_below="@+id/editText3" 
     android:ems="10" 
     android:inputType="textMultiLine" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignTop="@+id/editText4" 
     android:text="Comments:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="15dp" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="300dp" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/editText4" 
     android:layout_toLeftOf="@+id/editText4" 
     android:text="Done" 
     android:onClick="goToWeb(???);" /> 

</RelativeLayout> 

與此Java:

package com.example.request; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainRequest extends Activity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main_request, menu); 
     return true; 
    } 

    public void goToWeb() { 
     /* open webpage - how? */ 
    } 
} 

我想使這個POST請求http://www.example.com當他們單擊完成:

name: (content of @+id/editText1) 
addr: (content of @+id/editText2) 
phone: (content of @+id/editText3) 
comment: (content of @+id/editText4) 

我想打開手機的瀏覽器,並執行POST請求(如果有意義的話)

我該怎麼做?

+0

誰在嘲弄我'名稱代碼:H,電話:3,地址:J-,評論:了'它的工作:) – stackunderflow

+0

我剛剛拿到了'name:a,phone:2,addr:a,comment:j'和'name:a,phone:2,addr:a,comment:d' – stackunderflow

回答

1

試試這個類MainRequest

package com.example.teststack; 

import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainRequest extends Activity { 

    EditText textPersonName = null; 
    EditText textPostalAddress = null; 
    EditText phone = null; 
    EditText textMultiLine = null; 
    Button submit = null; 
    String action = "http://www.omokoroacomputerhelp.com/"; 
    HttpPost httpRequest = null; 
    List<NameValuePair> params = null; 
    HttpResponse httpResponse = null; 
    WebView webView = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_request); 
     textPersonName = (EditText) findViewById(R.id.personName); 
     textPostalAddress = (EditText) findViewById(R.id.postalAddress); 
     phone = (EditText) findViewById(R.id.phone); 
     textMultiLine = (EditText) findViewById(R.id.multiLine); 
     submit = (Button) findViewById(R.id.submit); 
     submit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       httpRequest = new HttpPost(action); 
       params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("name", textPersonName 
         .getText().toString())); 
       params.add(new BasicNameValuePair("phone", phone.getText() 
         .toString())); 
       params.add(new BasicNameValuePair("addr", textPostalAddress 
         .getText().toString())); 
       params.add(new BasicNameValuePair("comment", textMultiLine 
         .getText().toString())); 
       try { 
        // send http request 
        httpRequest.setEntity(new UrlEncodedFormEntity(params, 
          HTTP.UTF_8)); 
        // get http response 
        httpResponse = new DefaultHttpClient().execute(httpRequest); 
        // 
        Intent gotoIntent = new Intent(MainRequest.this, 
          Webpage.class); 
        gotoIntent.putExtra("source", 
          EntityUtils.toString(httpResponse.getEntity())); 
        startActivity(gotoIntent); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main_request, menu); 
     return true; 
    } 

} 

和網頁

package com.example.teststack; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.webkit.WebView; 

public class Webpage extends Activity { 
    WebView webView; 

    final String mimeType = "text/html"; 

    final String encoding = "utf-8"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_webpage); 
     Intent webPageIntent = getIntent(); 
     String htmlSource = webPageIntent.getStringExtra("source"); 
     webView = (WebView) findViewById(R.id.webview); 
     webView.loadData(htmlSource, mimeType, encoding); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_webpage, menu); 
     return true; 
    } 
} 

和activity_main_request.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <EditText 
     android:id="@+id/personName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:ems="10" 
     android:inputType="textPersonName" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Name:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/postalAddress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/personName" 
     android:layout_below="@+id/personName" 
     android:ems="10" 
     android:inputType="textPostalAddress" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignTop="@+id/postalAddress" 
     android:text="Address:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/phone" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/postalAddress" 
     android:ems="10" 
     android:inputType="phone" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/postalAddress" 
     android:text="Phone:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="20dp" /> 

    <EditText 
     android:id="@+id/multiLine" 
     android:layout_width="wrap_content" 
     android:layout_height="200dp" 
     android:layout_alignLeft="@+id/phone" 
     android:layout_below="@+id/phone" 
     android:ems="10" 
     android:inputType="textMultiLine" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignTop="@+id/multiLine" 
     android:text="Comments:" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="15dp" /> 

    <Button 
     android:id="@+id/submit" 
     android:layout_width="300dp" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/multiLine" 
     android:layout_toLeftOf="@+id/multiLine" 
     android:text="Done" /> 

</RelativeLayout> 

和activity_webpage.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <WebView 
     android:id="@+id/webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     /> 

</RelativeLayout> 

和AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.teststack" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainRequest" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Webpage" 
      android:label="@string/title_activity_webpage" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

謝謝!我得到的只是一個錯誤:'activity_webpage無法解析或不是字段' – stackunderflow

+0

您應該在項目中添加activity_webpage.xml,例如/ res/layout 還要在AndroidManifest.xml中添加以下文本 ' ' – wodong

+0

我已經這樣做了,仍然有錯誤.. – stackunderflow

1

我覺得這是一個很好的答案,你可能要考慮看​​看有關的問題。希望這可以幫助。

Java - sending HTTP parameters via POST method easily

編輯:其實我發現了一個以前更好了一個用於Android系統具體包括:

Android, Java: HTTP POST Request

好了,所以我想你也有點困惑如何從文本獲取文本你創建的字段。這裏是另一個計算器的問題,可以幫助你:

Get Value of a Edit Text field

你有一些的EditText領域設立所以你只要將它們存儲在一個變量(您可以通過使用findViewById(R.id.idOfEditText)並將其存儲在一個拿到價值變量。現在有了這個變量,你只需要調用.getText(),然後你必須字符串,可以做任何你需要用它做(例如通過郵遞)。

+0

對不起,但我還在苦苦掙扎,怎麼樣我從文本字段獲取值並將它們發佈? – stackunderflow

+0

我更新了我的答案。對不起,只是指出其他問題,但如果我覺得有另一個問題是問你需要什麼,那麼它是好的(他們通常解釋它比我更好),但如果你仍然需要幫助,不要害怕問 – aug

+0

我已經使用HTTP POST請求一個(沒有嘗試獲取文本框的數據)並獲得一個FC,當我點擊完成時。 – stackunderflow

0

嘗試使用NameValuePair ..我給我在我的應用程序用來做HTTP POST

public String postData(String url, String xmlQuery) { 



     final String urlStr = url; 
     final String xmlStr = xmlQuery; 
     final StringBuilder sb = new StringBuilder(); 


     Thread t1 = new Thread(new Runnable() { 

      public void run() { 

       HttpClient httpclient = MySSLSocketFactory.getNewHttpClient(); 

       HttpPost httppost = new HttpPost(urlStr); 


       try { 

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
          1); 
        nameValuePairs.add(new BasicNameValuePair("xml", xmlStr)); 

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response = httpclient.execute(httppost); 

        Log.d("Vivek", response.toString()); 

        HttpEntity entity = response.getEntity(); 
        InputStream i = entity.getContent(); 

        Log.d("Vivek", i.toString()); 
        InputStreamReader isr = new InputStreamReader(i); 

        BufferedReader br = new BufferedReader(isr); 

        String s = null; 


        while ((s = br.readLine()) != null) { 

         Log.d("YumZing", s); 
         sb.append(s); 
        } 


        Log.d("Check Now",sb+""); 




       } catch (ClientProtocolException e) { 

        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

     }); 

     t1.start(); 
     try { 
      t1.join(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     System.out.println("Getting from Post Data Method "+sb.toString()); 

     return sb.toString(); 
    }