【发布时间】:2014-03-04 17:40:58
【问题描述】:
嗨,
我使用这个类向服务器发出请求,该请求由 json 数据对象组成。 类是:-
public class HttpClient {
private static String URL = "localhost/json/json_handle.php";
public String postJsonData(String data) {
try {
StringBuffer buffer = new StringBuffer();
// Apache HTTP Reqeust
System.out.println("Sending data..");
System.out.println("Data: [" + data + "]");
org.apache.http.client.HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
List<NameValuePair> nvList = new ArrayList<NameValuePair>();
BasicNameValuePair bnvp = new BasicNameValuePair("json", data.toString());
// We can add more
nvList.add(bnvp);
post.setEntity(new UrlEncodedFormEntity(nvList));
HttpResponse resp = client.execute(post);
// We read the response
InputStream is = resp.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
is.close();
buffer.append(str.toString());
// Done!
return buffer.toString();
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
}
然后我在服务器端使用 php 类从请求中获取 json 对象。但是,在服务器端我什么也没得到。即使我使用$_REQUEST 方法,此方法之后的代码也不起作用。
这是我的 php 文件:-
<?php
$file = fopen("MyFile.txt" ,"w");
$int = $_REQUEST;
fwrite($file,"aaa");
//$input =$_REQUEST['json'];
fwrite($file,"HELLO 111");
//$data = json_decode($input,true);
/*print_r($input);
// get values
$firstname = $input->firstName;
$surename = $input->lastName;
$age = intval($input->age);
// check values
if (isset($firstname) && !empty($firstname) &&
isset($surename) && !empty($surename) &&
isset($age) && is_numeric($age))
{
// do something
echo "Hello ".htmlspecialchars($firstname)." ".htmlspecialchars($surename)."!<br>";
echo "You are $age years old! Wow.";
}
else
{
echo "Some values are missing or incorrect";
}*/
//fwrite($file, $data);
fclose($file);
?>
关于这个问题有什么建议吗???
【问题讨论】:
-
不起作用是什么意思?
-
您是否尝试使用
$_GET参数手动调用您的 php 并检查日志记录是否有效? -
我在 php 文件中使用文件处理。在 $int = $_REQUEST; 之后我的代码行不通,因为它没有创建任何文件。这意味着它给出了一个错误。
-
你为什么要做 $int = $_REQUEST?尝试使用 ini_set("display_errors", 1) 运行您的代码。这应该会给您导致此行为的错误。或者您在客户端代码中收到响应代码 500?
-
请查看我的回答她Android JSON example