php - Android JSON HttpClient 使用 HttpResponse 将数据发

我目前正在尝试将一些数据从 Android 应用程序发送到 php 服务器(两者都由我控制)。

在应用程序的一个表单上收集了很多数据,这些数据被写入数据库。这一切都有效。

在我的主要代码中,首先我创建了一个 JSONObject(我在此示例中已将其删减):

JSONObject j = new JSONObject();
j.put("engineer", "me");
j.put("date", "today");
j.put("fuel", "full");
j.put("car", "mine");
j.put("distance", "miles");

接下来我将对象传递给发送,并接收响应:

String url = "http://www.server.com/thisfile.php";
HttpResponse re = HTTPPoster.doPost(url, j);
String temp = EntityUtils.toString(re.getEntity());
if (temp.compareTo("SUCCESS")==0)
{
    Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
}

HTTPPoster 类:

public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    HttpEntity entity;
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    entity = s;
    request.setEntity(entity);
    HttpResponse response;
    response = httpclient.execute(request);
    return response;
}

这会得到响应,但服务器正在返回 403 - Forbidden 响应。

我尝试稍微更改 doPost 函数(这实际上更好一点,正如我所说的,我有很多要发送的,基本上是 3 个具有不同数据的相同表单 - 所以我创建了 3 个 JSONObjects,每个表单条目一个- 条目来自数据库,而不是我正在使用的静态示例)。

首先我稍微改变了调用:

String url = "http://www.myserver.com/ServiceMatalan.php";
Map<String, String> kvPairs = new HashMap<String, String>();
kvPairs.put("vehicle", j.toString());
// Normally I would pass two more JSONObjects.....
HttpResponse re = HTTPPoster.doPost(url, kvPairs);
String temp = EntityUtils.toString(re.getEntity());
if (temp.compareTo("SUCCESS")==0)
{
    Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
}

好的,对 doPost 函数的更改:

public static HttpResponse doPost(String url, Map<String, String> kvPairs) throws ClientProtocolException, IOException 
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    if (kvPairs != null && kvPairs.isEmpty() == false) 
    {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(kvPairs.size());
        String k, v;
        Iterator<String> itKeys = kvPairs.keySet().iterator();
        while (itKeys.hasNext()) 
        {
            k = itKeys.next();
            v = kvPairs.get(k);
            nameValuePairs.add(new BasicNameValuePair(k, v));
        }             
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    }
    HttpResponse response;
    response = httpclient.execute(httppost);
    return response;
}

Ok 所以这会返回响应 200

int statusCode = re.getStatusLine().getStatusCode();

但是,服务器上接收到的数据无法解析为 JSON 字符串。我认为它的格式很糟糕(这是我第一次使用 JSON):

如果在 php 文件中我对 $_POST['vehicle'] 进行回显,我会得到以下信息:

{\"date\":\"today\",\"engineer\":\"me\"}

谁能告诉我哪里出了问题,或者是否有更好的方法来实现我想要做的事情?希望以上内容有意义!

最佳答案

经过大量阅读和搜索,我发现了问题所在,我相信在服务器上启用了 magic_quotes_gpc。

因此,使用:

json_decode(stripslashes($_POST['vehicle']));

在我上面的示例中,删除了斜杠并允许正确解码 JSON。

仍然不确定为什么发送 StringEntity 会导致 403 错误?

关于php - Android JSON HttpClient 使用 HttpResponse 将数据发送到 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2540786/

相关文章:

c# - WebApi Put如何从指定的属性中告诉未指定的属性设置为null?

javascript - 无法重新初始化 JQuery DataTable

javascript - 通过 AJAX 和 jQuery 从 PHP 数组中获取数据

c# - 使用注释将嵌套的 JSON 结构反序列化为使用 Json.NET 的扁平类

java - jackson 在通用列表中读取 json

eclipse - Jersey :找不到 Java 类和 MIME 媒体类型应用程序/json 的

javascript - 如何处理奇怪组合的 websocket 消息?

c# - Json空数组在MVC中反序列化为null

c# - 分配给单个属性的多个 JsonProperty 名称

html - "modern"浏览器 "handle"一次可以有多少个 HTML 元素?