restful 示例_Android Restful Web服务客户端示例

restful 示例

In this tutorial you learn how to create an android restful web service client to consume a restful web service developed in Java.

在本教程中,您将学习如何创建一个Android Restful Web Service客户端来使用Java开发的Restful Web Service。

Before making the android client make sure the web service is already running on server. I am using apache tomcat server to run the java web service in my local system.

在制作android客户端之前,请确保网络服务已在服务器上运行。 我正在使用Apache tomcat服务器在本地系统中运行Java Web服务。

If you don’t know how to make Java restful web service then read below tutorial.

如果您不知道如何使Java成为宁静的Web服务,请阅读以下教程。

Also Read: Create Simple Java RESTful Web Services Using Jersey

另请阅读: 使用Jersey创建简单的Java RESTful Web服务

In this example the android client will send some string to server which is reversed and sent back to the android client.

在此示例中,android客户端将向服务器发送一些字符串,该字符串将被反向并发送回android客户端。

Android Restful Web服务客户端示例 (Android Restful Web Service Client Example)

1. Create a new android project with package name com.androidrestfullwebservice

1.使用包名称com.androidrestfullwebservice创建一个新的android项目

2. I am using Volley library to load data from server. If you don’t know how to use this library then read below tutorial.

2.我正在使用Volley库从服务器加载数据。 如果您不知道如何使用该库,请阅读以下教程。

Also Read: Android Volley Tutorial With Example

另请阅读: Android Volley教程示例

3. Just copy and paste below line in build.gradle (Module) file to add dependency for Volley library. After adding Sync the project.

3.只需复制并粘贴到build.gradle(模块)文件中的行下方,即可添加Volley库的依赖项。 添加后同步项目。

compile 'com.android.volley:volley:1.0.0'

4. As we are doing network related work so add internet access permission in AndroidMainifest.xml file by adding following line.

4.在进行网络相关工作时,请通过添加以下行在AndroidMainifest.xml文件中添加互联网访问权限。

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

5. Create an activity and add following code in respective files.

5.创建一个活动,并在相应的文件中添加以下代码。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidrestfullwebservice.MainActivity"
    android:orientation="vertical">
 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fetch"
        android:id="@+id/btn"/>
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>
</LinearLayout>

MainActivity.java

MainActivity.java

package com.androidrestfullwebservice;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
 
public class MainActivity extends AppCompatActivity {
    EditText editText;
    TextView text;
    Button btn;
    String url ="http://192.168.1.3/JavaRESTfullWS/rest/DemoService/";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText = (EditText)findViewById(R.id.editText);
        text = (TextView)findViewById(R.id.text);
        btn = (Button)findViewById(R.id.btn);
 
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest request = new StringRequest(Request.Method.GET, url+editText.getText().toString(), new Response.Listener<String>(){
                    @Override
                    public void onResponse(String s) {
                        text.setText(s);
                    }
                },new Response.ErrorListener(){
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        text.setText("Some error occurred!!");
                    }
                });
 
                RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
                rQueue.add(request);
            }
        });
    }
}

Note: Make sure to replace 192.168.1.3 with your system IPv4. You can find it by running ipconfig command in command prompt.

注意:确保用系统IPv4替换192.168.1.3 。 您可以通过在命令提示符下运行ipconfig命令来找到它。

6. Now run the project.

6.现在运行项目。

Screenshot

屏幕截图

Android Restful Web Service Client Example

Comment below if you are facing any difficulty to make android restful web service client.

如果您在使android restful web服务客户端方面遇到任何困难,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/09/android-restful-web-service-client-example.html

restful 示例

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值