Android JSON HttpClient使用HttpResponse发送数据到PHP服务器

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

应用程序中的表单上收集了大量数据,将其写入数据库。 这一切工作。

在我的主要代码中,首先我创build一个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 – 禁止响应。

我试着改变doPost函数一点(这实际上是一个更好的,因为我说我有很多发送,基本上是3不同的数据相同的forms – 所以我创build了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; } 

好所以这返回一个响应200

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

但是,服务器上收到的数据不能被parsing为JSONstring。 这是格式严重,我认为(这是我第一次使用JSON):

如果在php文件中,我在$ _POST ['vehicle']上做了一个回声,我得到以下内容:

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

任何人都可以告诉我哪里错了吗?还是有更好的方法来实现我所要做的? 希望以上是有道理的!

经过大量的阅读和search,我发现问题是,我相信在服务器上启用magic_quotes_gpc。

因此,使用:

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

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

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

 StringEntity s = new StringEntity(c.toString()); s.setContentEncoding("UTF-8"); s.setContentType("application/json"); request.setEntity(s); 

试试这个代码对我有用

 public void postData(String result,JSONObject obj) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, 10000); HttpConnectionParams.setSoTimeout(myParams, 10000); String json=obj.toString(); try { HttpPost httppost = new HttpPost(result.toString()); httppost.setHeader("Content-type", "application/json"); StringEntity se = new StringEntity(obj.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String temp = EntityUtils.toString(response.getEntity()); Log.i("tag", temp); } catch (ClientProtocolException e) { } catch (IOException e) { } 

}

更改

 (String url = "http://www.server.com/MainPage.php";) 

 (String url = "http://www.server.com/MainPage.php?";) 

当您尝试将参数发送到php脚本时,最后的问号是必需的。

试试这个代码,它完美的作品

 *For HttpClient class* download jar file "httpclient-4.3.6.jar" and put in libs folder then Compile: dependencies {compile files('libs/httpclient-4.3.6.jar')} repositories { maven { url "https://jitpack.io" } }