2015-08-27 2 views
2

Я использую студию android. Я пытаюсь использовать веб-службы в студии Android, но не смог найти учебник или помочь использовать код в студии android. В eclipse GetText() используется для вызова метода POST. Что я использую в андроид-студии вместо этого метода? Может ли кто-нибудь помочь мне отправить рабочий код, который извлекает данные с сервера, а также отправляет запрос на сервер с использованием методов POST и GET в студии Android. Есть ли другой альтернативный способ вызова веб-сервисов в android?как мы можем сделать метод вызова в андроид-студии

Вот мой 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> 

ответ

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

    } 
Смежные вопросы