的JSON的HttpClient与Htt的presponse将数

的JSON的HttpClient与Htt的presponse将数

本文介绍了Android的JSON的HttpClient与Htt的presponse将数据发送到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在试图从一些数据和Android应用程序发送到PHP服务器(无论是由我控制)。

I am currently trying to send some data from and Android application to a php server (both are controlled by me).

有很多收集在该应用窗体上的数据,这被写入到数据库中。这一切工作。

There is alot of data collected on a form in the app, this is written to the database. This all works.

在我的主要code,首先我将创建一个JSONObject(我已经剪下来这里在这个例子中):

In my main code, firstly I create a JSONObject (I have cut it down here for this example):

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

接下来,我传递对象在发送和接收响应:

Next I pass the object over for sending, and receive the response:

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类:

The HTTPPoster class:

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 - 禁止响应

This gets a response, but the server is returning a 403 - Forbidden response.

我曾试图改变的doPost功能一点(其实这是好一点,因为我说我有很多派,基本上3相同的形式有不同的数据 - 所以我创建3个JSONObjects,每个表单输入 - 条目来我使用的是静态的例子从数据库代替)

I have tried changing the doPost function a little (this is actually a little better, as I said I have alot to send, basically 3 of the same form with different data - so I create 3 JSONObjects, one for each form entry - the entries come from the DB instead of the static example I am using).

首先,我换了电话过了一下:

Firstly I changed the call over a bit:

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功能:

Ok so the changes to the doPost function:

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;
}

好了,所以这个返回响应200

Ok So this returns a response 200

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

然而在服务器上接收到的数据不能被解析到一个JSON字符串。这是格式错误,我认为(这是我第一次使用JSON):

However the data received on the server cannot be parsed to a JSON string. It is badly formatted I think (this is the first time I have used JSON):

如果在PHP文件我做$ _ POST回音['车']我得到了以下内容:

If in the php file I do an echo on $_POST['vehicle'] I get the following:

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

谁能告诉我在哪里,我错了,或者有更好的方法来实现我想要做什么?希望上面的有道理!

Can anyone tell me where I am going wrong, or if there is a better way to achieve what I am trying to do? Hopefully the above makes sense!

推荐答案

在大量的阅读和搜索,我发现这个问题是有,我beleive magic_quotes_gpc的服务器上被启用。

After lots of reading and searching I have found the problem to be with, I beleive magic_quotes_gpc being enabled on the server.

因此​​,使用:

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

在我上面的例子中删除斜线,并允许JSON是德codeD正常。

In my example above removes the slashes and allows the JSON to be decoded properly.

不过不知道为什么发送StringEntity导致403错误?

Still not sure why sending a StringEntity causes a 403 error?

这篇关于Android的JSON的HttpClient与Htt的presponse将数据发送到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 04:36