如何从Android调用基于PHP的Web服务?

时间:2022-10-30 23:23:18

I have a simple PHP web-service which return result as JSON .

我有一个简单的PHP Web服务,它返回结果为JSON。

if($_SERVER["REQUEST_METHOD"]=="POST"){
    $arg1=$_POST["arg1"];
    processArgs($arg1);
}

processArgs($arg1){
    $result=doSomething($arg1);
    echo json_encode($result);
}

I could call it from Android side using HttpURLConnection. But the problem is HttpURLConnection seems to be a work in very low level. Is there any level implementation which we could avoid writing the same code for making it asynchronous and for parsing the result.

我可以使用HttpURLConnection从Android端调用它。但问题是HttpURLConnection似乎是一个非常低级别的工作。是否存在任何级别实现,我们可以避免编写相同的代码以使其异步并解析结果。

2 个解决方案

#1


0  

Simply use HttpClient as follows.

只需使用HttpClient,如下所示。

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://your-host/comm.php");

    try {
        //add your post data
        List<NameValuePair> args = new ArrayList<NameValuePair>(2);
        args.add(new BasicNameValuePair("arg1", "your-arg-1"));

        httpPost.setEntity(new UrlEncodedFormEntity(args));

        //send your request
        HttpResponse response = httpClient.execute(httpPost);

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

#2


0  

HttpURLConnection (java.net.HttpURLConnection) is the default HTTP client in Android.

HttpURLConnection(java.net.HttpURLConnection)是Android中的默认HTTP客户端。

OkHttp, is another one which became the engine that powers HttpUrlConnection as of Android 4.4. It is offers easier method to customize each requests.

OkHttp,是另一个从Android 4.4开始为HttpUrlConnection提供动力的引擎。它提供了更简单的方法来自定义每个请求。

Both HttpURLConnection and OkHttp works at low level. So we need to write our own code for making it asynchronous and for parsing the result again and again.

HttpURLConnection和OkHttp都在低级别工作。所以我们需要编写自己的代码以使其异步并一次又一次地解析结果。

Retrofit, on the other hand, is a high level implementation which uses OkHttp for connection and Gson for parsing result. It can be used to turn HTTP APIs into a Java interface with ease.

另一方面,Retrofit是一个高级实现,它使用OkHttp进行连接,使用Gson进行解析结果。它可以用来轻松地将HTTP API转换为Java接口。

Therefore, Retrofit would be the best choice unless you have specific reason to go with OkHttp ( Like HTTP-based streaming).

因此,除非您有特定的理由选择OkHttp(与基于HTTP的流式传输),否则Retrofit将是最佳选择。

#1


0  

Simply use HttpClient as follows.

只需使用HttpClient,如下所示。

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://your-host/comm.php");

    try {
        //add your post data
        List<NameValuePair> args = new ArrayList<NameValuePair>(2);
        args.add(new BasicNameValuePair("arg1", "your-arg-1"));

        httpPost.setEntity(new UrlEncodedFormEntity(args));

        //send your request
        HttpResponse response = httpClient.execute(httpPost);

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

#2


0  

HttpURLConnection (java.net.HttpURLConnection) is the default HTTP client in Android.

HttpURLConnection(java.net.HttpURLConnection)是Android中的默认HTTP客户端。

OkHttp, is another one which became the engine that powers HttpUrlConnection as of Android 4.4. It is offers easier method to customize each requests.

OkHttp,是另一个从Android 4.4开始为HttpUrlConnection提供动力的引擎。它提供了更简单的方法来自定义每个请求。

Both HttpURLConnection and OkHttp works at low level. So we need to write our own code for making it asynchronous and for parsing the result again and again.

HttpURLConnection和OkHttp都在低级别工作。所以我们需要编写自己的代码以使其异步并一次又一次地解析结果。

Retrofit, on the other hand, is a high level implementation which uses OkHttp for connection and Gson for parsing result. It can be used to turn HTTP APIs into a Java interface with ease.

另一方面,Retrofit是一个高级实现,它使用OkHttp进行连接,使用Gson进行解析结果。它可以用来轻松地将HTTP API转换为Java接口。

Therefore, Retrofit would be the best choice unless you have specific reason to go with OkHttp ( Like HTTP-based streaming).

因此,除非您有特定的理由选择OkHttp(与基于HTTP的流式传输),否则Retrofit将是最佳选择。