Java(Android)HTTP Post到PHP页面
这是针对Android应用程序的,我的移动开发人员正在尝试将json数据发布到我的PHP页面 下面是正在使用的功能:Java(Android)HTTP Post到PHP页面,java,php,android,Java,Php,Android,这是针对Android应用程序的,我的移动开发人员正在尝试将json数据发布到我的PHP页面 下面是正在使用的功能: public static String postData(String url, String postData) { // Create a new HttpClient and Post Header InputStream is = null; StringBuilder sb = null; String result = ""; /
public static String postData(String url, String postData) {
// Create a new HttpClient and Post Header
InputStream is = null;
StringBuilder sb = null;
String result = "";
// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
// StrictMode.setThreadPolicy(policy);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
httppost.setEntity(new StringEntity(postData));
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
//throw new CustomException("Could not establish network connection");
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
//throw new CustomException("Error parsing the response");
}
return result;
}
其中url是指向我的php网页的链接。在我的php页面上,我正试图通过以下操作打印出发布的数据:
print_r($_POST);
但它显示了一个空数组。我甚至尝试过使用firefox的REST插件,但它只是显示了一个空白数组
如果有人能指出我遗漏了什么,那就太好了
谢谢。您需要获得如下所示的json内容:
if(isset($_POST))
{
$json = file_get_contents('php://input');
$jsonObj = json_decode($json);
echo $jsonObj;
}