【问题标题】:What is the best way to send a name value pair to a server?将名称值对发送到服务器的最佳方式是什么?
【发布时间】:2012-03-14 01:34:19
【问题描述】:

两周来我一直在尝试这个,但我真的被困住了。最初我创建了一个简单的 ObjectOutputStream 客户端 - 服务器程序 - 客户端是 Android 应用程序,但它不起作用(它读取连接而不是对象)。

所以现在我很困惑我可以采取哪些其他方法来执行这个简单的任务?谁能帮忙?

【问题讨论】:

  • 对服务器有什么要求吗?否则我会考虑使用 HTTP 服务器并使用该协议来回发送数据。

标签: android server client


【解决方案1】:

您可以使用它向服务器发布实体

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(url);
    postRequest.setEntity(entity);
    try {

        HttpResponse response = httpClient.execute(postRequest
                );
        String jsonString = EntityUtils.toString(response
                .getEntity());
        Log.v(ProgramConstants.TAG, "after uploading file "
                + jsonString);
        return jsonString;

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

实体可以是名称值对:

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("key1", value1));
nvps.add(new BasicNameValuePair("key2", value2));
Entity entity=new UrlEncodedFormEntity(nvps, HTTP.UTF_8)  

或者你可以发送一个带有字节数组的实体。

Bitmap bitmapOrg=getBitmapResource();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

byte[] data = bao.toByteArray();

MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
entity.addPart("file", new ByteArrayBody(data, "image/jpeg",
                        "file"));

如果您想将 json 发布到服务器:
请查看此链接How do I send JSon as BODY In a POST request to server from an Android application?
对于java对象的序列化和反序列化,我推荐https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson 真的希望它可以帮助您了解向服务器发送数据的概述

【讨论】:

  • 谢谢,我将研究发往服务器的帖子并尝试编写一个 servlet。哈希表也可以是实体吗?
  • 是的,您可以从哈希表构建一个 NameValuePair 对象。然后用该 NameValuePair 对象构造 UrlEncodedFormEntity
【解决方案2】:

你为什么不试试这个:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

【讨论】:

  • 我也推荐 Json 来解析数据
  • 所以我可以将一个对象(名称值对)发布到服务器并从 servlet 中读取它?这可以与哈希表一起使用吗?
  • 如果您的对象数据只有几个字段,例如 ProductID、Price,那么,我认为您可以将它们作为键值对发送。但是,如果您有很多字段,我相信使用 json 并让库为您在服务器上重建对象。 :)
【解决方案3】:

您可以尝试使用 JSON 搅拌来发送数据。我们有很多关于如何使用 JSON 的资料,也有很多 api。 JSONSimple 是我可以建议的。真的很简单。

【讨论】:

  • 我一直在看JSON,但没有找到我能理解的教程,它们看起来都很复杂。
  • 如果你想将一个java对象序列化为json。我建议 Gson sites.google.com/site/gson/gson-user-guide。这真的很好,尤其是在从 json 反序列化为 java 对象时。
【解决方案4】:

您是否尝试过使用 post 方法的 URLConnection? :)

或者像这样的get方法:

String yourURL = "www.yourwebserver.com?value1=one&value2=two";
URL url = new URL(yourURL);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
response = in.readLine();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2015-09-17
    • 2010-11-04
    相关资源
    最近更新 更多