[英]Android: Getting Empty String From Php webpage
我有两个申请,一个很长。 和纬度坐标到一个php文件,另一个应用程序检索long。 和拉特。 坐标。 为了测试并查看是否可以开始工作,我创建了一个函数,并发布了纬度和经度。 协调两个php服务,我又将它们恢复到同一应用程序中。 我把它们放在烤面包片上看是否有效。 我什至实现了位置侦听器,以上传坐标并在同一应用程序中检索它们以进行测试,然后再尝试在其他应用程序中接收它们。 工作正常。 但是,当我尝试在其他应用程序中使用相同的代码来接收坐标时,会收到空白坐标。 我调试了它,只是将其调试为空白,好像当我从另一个应用程序对服务器进行调用时,它清除了php服务中的当前值。
在应用程序一中放置坐标的代码:
public void postData(String longCord, String latCord) throws JSONException{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(".../android/serverFile.php");
JSONObject json = new JSONObject();
try {
// JSON data:
json.put("longitude", longCord);
json.put("latitude", latCord);
JSONArray postjson=new JSONArray();
postjson.put(json);
// Post the data:
httppost.setHeader("json",json.toString());
httppost.getParams().setParameter("jsonpost",postjson);
// Execute HTTP Post Request
//System.out.print(json);
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
JSONObject jsonObj = new JSONObject(jsonStr);
// grabbing the menu object
String longitudecord = jsonObj.getString("lon");
String latitudecord = jsonObj.getString("lat");
Toast.makeText( getApplicationContext(),longitudecord,Toast.LENGTH_SHORT).show();
Toast.makeText( getApplicationContext(),latitudecord,Toast.LENGTH_SHORT).show();
}
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
php文件:
<?php
$json = $_SERVER['HTTP_JSON'];
$data = json_decode($json);
$lon = $data->longitude;
$lat = $data->latitude;
$variable = array( 'lon' => "$lon", 'lat' => "$lat" );
// One JSON for both variables
echo json_encode($variable);
?>
现在,当我在另一个应用程序上运行此代码时,...与上面的代码相同,减去了坐标...我得到lon:“”和lat:“”。 有点像通过发出请求删除了其他应用程序发布的信息。 是这样吗
public void recieveData() throws JSONException{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(".../android/serverFile.php");
try {
HttpResponse response = httpclient.execute(httppost);
// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String jsonStr = sb.toString();
JSONObject jsonObj = new JSONObject(jsonStr);
// grabbing the menu object
String longitudecord = jsonObj.getString("lon");
String latitudecord = jsonObj.getString("lat");
Toast.makeText( getApplicationContext(),longitudecord,Toast.LENGTH_SHORT).show();
Toast.makeText( getApplicationContext(),jsonStr,Toast.LENGTH_SHORT).show();
}
尝试在浏览器中访问您的URL,并确保您的PHP脚本在以下各行中没有出现任何错误(因为在第二个应用程序中未提供任何JSON输入):
$data = json_decode($json);
$lon = $data->longitude;
$lat = $data->latitude;
另外,很高兴听到您如何将经 /纬度保存在服务器上,以便它们在两次调用之间保持不变。
例如,您可以使用以下PHP代码:
$db_host = "localhost";
$db_name = "********";
$db_user = "********";
$db_pass = "********";
// connect to MySQL
$db = mysql_connect($db_host, $db_user, $db_pass);
if( !$db ) {
die( 'Unable to connect to server : ' . mysql_error() );
}
// select the DB
if( !mysql_select_db($db_name) ) {
die( 'Unable to select DB : ' . mysql_error() );
}
// create table, if not exists
if( !mysql_query( "CREATE TABLE IF NOT EXISTS `locations` (
`id` int(11) NOT NULL auto_increment,
`latitude` double NOT NULL COMMENT 'latitude',
`longitude` double NOT NULL COMMENT 'longitude',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
) ) {
die( 'Unable to create table : ' . mysql_error() );
}
// save values
mysql_query( "INSERT INTO locations (`latitude`, `longitude`) VALUES ( '$lat', '$lon')" );
// read them back
$track = array()
$result = mysql_query( "SELECT * FROM locations" );
while( $data = mysql_fetch_assoc($result) ) {
$track[] = array(
'latitude' => $data['latitude'],
'longitude' => $data['longitude'] );
}
// here you may convert `track` into JSON / XML and send coordinates list to your application
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.