android如何利用基于Http 协议的WebService服务来获取远程数据库数据

4 篇文章 0 订阅
2 篇文章 0 订阅
初学android,这个问题困扰了多天,在搜索引擎及论坛各位高手的帮助下,现在终于给搞定了,在这里分享给大家,希望对大家有所帮助!(其实本来是尝试用SOAP协议的,试了n天,无果,无奈又只能回到http了!)先来看看服务器端吧!

首先新建一个ASP.NET Web服务应用程序,我这里将其命名为ShopService,具体代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using BLL.Shop;
using System.Web.Script.Services;

namespace WebService
{
/// <summary>
/// ShopService 的摘要说明
/// </summary>
[WebService(Namespace = "http://具体的根据你的需要咯/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]

public class ShopService : System.Web.Services.WebService
{
[WebMethod]
//[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getShopItems(string userId, int type)
{
Shop shop = new Shop();
return shop.getShopItems(userId, type);
}
}
}

然后再来看看android客户端,我就直接上代码了
package com.xxx;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class WebserviceTookit {
// private static final String TAG = "ShopItems";
private static final String WEBSERVICE_IP = "http://10.0.2.2:14294";

public static String getShopItems(String userId, int type) {
try {
// 调用带参数的WebMethod
final String SERVER_URL = WEBSERVICE_IP
+ "/ShopService.asmx/getShopItems"; // 带参数的WebMethod
HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求
request.addHeader("Content-Type", "application/json; charset=utf-8");// 必须要添加该Http头才能调用WebMethod时返回JSON数据
JSONObject jsonParams = new JSONObject();
jsonParams.put("userId", userId);// 传参
jsonParams.put("type", type);
HttpEntity bodyEntity = new StringEntity(jsonParams.toString(),
"utf8");// 参数必须也得是JSON数据格式的字符串才能传递到服务器端,否则会出现"{'Message':'xxx是无效的JSON基元'}"的错误
request.setEntity(bodyEntity);

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is
// established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3 * 1000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5 * 1000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse httpResponse = httpClient.execute(request); // 发送请求并获取反馈
// 解析返回的内容
if (httpResponse.getStatusLine().getStatusCode() == 200) // StatusCode为200表示与服务端连接成功
{
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader2 = new BufferedReader(
new InputStreamReader(httpResponse.getEntity()
.getContent()));
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine()) {
builder.append(s);
}

String resultString = builder.toString();
resultString = JSONToolkit.removeEscape(resultString);

return resultString;
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
return "";
}
}


运行时记得关闭防火墙或者添加例外端口~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值