두 개의 응용 프로그램이 있습니다. 하나는 오래 전송합니다. php 파일에 위도 좌표와 다른 응용 프로그램은 long을 검색합니다. 및 위도. coords. 테스트하고 내가 첫 번째 작업을 할 수 있는지 확인하기 위해 위도와 경도를 게시 한 함수를 만듭니다. coords 두 PHP 서비스와 나는 그들에게 같은 응용 프로그램에서 다시있어. 나는 그것이 작동하는지보기 위해 건배에 그들을 배치했다. 필자는 위치 수신기를 구현하여 좌표를 업로드하고 동일한 응용 프로그램에서 검색하여 다른 응용 프로그램에서 수신하려고 시도하기 전에 테스트했습니다. 그것은 잘 작동합니다. 그러나 좌표를 받기 위해 다른 응용 프로그램에서 동일한 코드를 사용하려고하면 빈 좌표가 나타납니다. 내가 그것을 디버깅하고 방금 빈 것처럼, 내가 다른 응용 프로그램에서 서버에 전화를 걸면 PHP 서비스의 현재 값을 지 웁니다.Android : 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);
지금>
내가 다른 응용 프로그램에서이 코드를 실행 ... 그것? 위의 좌표와 같지 않은 좌표와 같습니다. 나는 ""와 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();
}
가 ".../안드로이드/serverFile.php"나는이 사이트에 elipses를 넣어 – lenik
나에게 유효한 URL처럼 보이게하지 않습니다 그래서 I 내 파일이 호스팅 된 곳을 공유하지 않습니다 ... URL이 작동합니다. 첫 번째 신청서를 게시하고 가져 오는 중입니다. 두 번째는 공백이되는 곳입니다. – inspired
귀하의 데스크톱 브라우저에서 해당 URL을 치고 페이지에 무엇이 있는지 확인하십시오 – Akram