2012-12-28 2 views
0

온라인 PHP 스크립트를 읽고 다른 결과를 가진 JSON 배열을 반향하는 Android 앱을 개발 중입니다. 나는 또한 응용 프로그램에서 PHP 스크립트로 POST 변수를 보냈습니다. 웹 사이트에서 스크립트에 게시 할 때 제대로 작동하고 POST 데이터가 있습니다. 앱에서 게시 할 때 POST 데이터가 비어 있습니다.

private JSONArray addBusiness(String businessName, String businessCategory, String businessDescription) { 
    String result = ""; 
    InputStream is = null; 
    try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost("http://somesite.com/testAndroid.php"); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
     nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 
     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // 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()); 
    } 

    try { 
     Log.e("log_tag", "INSIDE TRY/CATCH"); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
     Log.e("log_tag", "RESULT: " + result); 
    } catch (Exception e) { 
     Log.e("log_tag", "Error converting result " + e.toString()); 
    } 

    try { 
     return new JSONArray(result); 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 
    return null; 
} 

다음은 비어있는 PHP 코드입니다. 어떤 아이디어? 또한, 나는 GoDaddy와 공유 리눅스는 PHP 5.2

<?php 
$return = array(); 
$return['result'] = 'pooping for the Internet'; 
$return['business'] = 'BUSINESS'; 
$return['post'] = $_POST['id']; 
echo '['.json_encode($return).']'; 
?> 

다른 모든 $ 리턴 데이터가 '포스트'반환을 제외하고 올바른 실행 호스팅 사용합니다. 생각?

+0

나머지가 나오면 문제는 '$ _POST'에있는 것처럼 보입니다. 게시가 제대로되었는지 확인하십시오. 게시물이 제대로 전달되는지 테스트 해보십시오. – cjds

답변

2

모든 정보 주셔서 감사합니다. 문제는 내 .htaccess 파일에 있습니다. 내가 URL 앞에 "www"를 넣는 재 작성 규칙을 잊어 버렸습니다. 나는 POST 데이터에서 잃어버린 원인 인 "www"가없는 상태에서 내 사이트에 POST하려고했습니다. 서브 디렉토리에 대한 .htaccess 파일을 덮어 썼는데, 이제는 모든 것이 잘 작동합니다 !!! 팁에 다시 한번 감사드립니다!

0

귀하의 앱이 PHP 코드의 HTTP 요청을 생성합니까?

Perl이나 Python을 사용하는 것과 마찬가지로 PHP (CLI-Mode)을 해석 할 수 있습니다 (HTTP 프로토콜 1이 포함되어 있지 않은 경우). 스크립트가 웹 서버를 통해 전달되는 경우 실제로는 $_POST, $_GET$_REQUEST 배열이어야합니다.

this을 사용하여 http 헤더가 있는지 여부를 확인할 수 있습니다.

관련 문제