how do we make post method call in android studio

Refresh

November 2018

Views

1.2k time

2

I am using the android studio. I am trying to use the web services in android studio but couldn't find any tutorial or helping code to use in android studio. In eclipse, GetText() is used to make POST method call. What do I use in android studio instead of that method? Can anyone help me send a working code that fetches the data from the server and also sends the request to the server using POST and GET method in android studio. Is there any other alternative way to call web services in android??

Here is my xml design

<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>

1 answers

0

Этот простой фрагмент кода может быть хорошей отправной точкой, когда вы пытаетесь создать запрос POST:

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();
        }

    }