【问题标题】:how do we make post method call in android studio我们如何在 android studio 中调用 post 方法
【发布时间】:2015-08-27 06:21:39
【问题描述】:

我正在使用安卓工作室。我正在尝试在 android studio 中使用 web 服务,但找不到任何教程或帮助代码在 android studio 中使用。在 Eclipse 中,GetText() 用于进行 POST 方法调用。我在 android studio 中使用什么而不是那种方法?谁能帮我发送一个从服务器获取数据的工作代码,并在android studio中使用POST和GET方法将请求发送到服务器。有没有其他替代方法可以在 android 中调用 Web 服务?

这是我的 xml 设计

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HttpPostExample">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:orientation="vertical"
    android:background="#FF9696">

    <TextView android:id="@+id/content"
        android:text="@string/op"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="#995A5A">

</View>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:background="#FFC2A3">

    <Button android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_save"
        android:onClick="saveOnWeb"/>

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="#995A5A">

</View>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="#F1C6B8">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/name"/>

        <EditText android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/email"/>

        <EditText android:id="@+id/e-mail"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/login"/>

        <EditText android:id="@+id/login-name"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/pass"/>

        <EditText android:id="@+id/pass-word"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android web-services post android-studio get


    【解决方案1】:

    当您尝试创建 POST 请求时,这个简单的 sn-p 代码可能是一个很好的起点:

    private void makePostRequest() {
    
    
            HttpClient httpClient = new DefaultHttpClient();
                                    // replace with your url
            HttpPost httpPost = new HttpPost("www.example.com"); 
    
    
            //Post Data
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
            nameValuePair.add(new BasicNameValuePair("username", "test_user"));
            nameValuePair.add(new BasicNameValuePair("password", "123456789"));
    
    
            //Encoding POST data
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            } catch (UnsupportedEncodingException e) {
                // log exception
                e.printStackTrace();
            }
    
            //making POST request.
            try {
                HttpResponse response = httpClient.execute(httpPost);
                // write response to log
                Log.d("Http Post Response:", response.toString());
            } catch (ClientProtocolException e) {
                // Log exception
                e.printStackTrace();
            } catch (IOException e) {
                // Log exception
                e.printStackTrace();
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2018-07-29
      • 1970-01-01
      相关资源
      最近更新 更多