2012-12-02 3 views
0

안드로이드 응용 프로그램은 문제없이 JSON 개체를 보내는 것 같아하지만 난받을 때 내가 얻을 :앱에서 JSON 데이터를 수신 할 수 없습니다

"공지 사항 : 정의되지 않은 인덱스를"

개체를 보내는 코드가 여기에 있습니다 :

public void sendJson(String name1, String name2) throws JSONException { 


    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://example.com/JSON_FOLDER/JSON2/parseData.php"); 
    HttpResponse response; 

    JSONObject json = new JSONObject(); 

    try {   
      json.put("name1", name1); 
      json.put("name2", name2); 

      StringEntity se = new StringEntity(json.toString()); 
      se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
      httppost.getParams().setParameter("json", json);  // new code 

      //Execute HTTP POST request 

      httppost.setEntity(se); 
      response = httpclient.execute(httppost); 

      if(response != null) { 
        str = inputStreamToString(response.getEntity().getContent()).toString(); 
        Log.i("DATA", "Data send== " + str); 
      } 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


} 
서버 측에서

:

$json = $_POST['name1']; 
$decoded = json_decode($json, TRUE); 

와 나는 정의되지 않은 인덱스 통지를 얻었다.

+0

내가 서버 나 클라이언트 측에서 중, 당신이 대답 원시 결과를 살펴 보시기 바랍니다. – arkascha

+0

처럼 : *** file_get_contents ('php : // input'); *** 시도했지만 결과가 없습니다 – t0s

+0

당신은 로그 파일 (예 : PHP syslog())에 응답을 덤프하는 서버쪽에 있습니다. 클라이언트 측에서 : 귀하의 플랫폼에 따라 달라집니다, 나는 안드로이드를 모른다. 아마도 Wireshark와 같은 네트워크 스니퍼를 사용하는 것이 가장 쉽습니다. – arkascha

답변

0

편집 - 내 대답을 수정 :

당신이 데이터와 'NAME1'및 'NAME2'를 포함 json라는 하나의 매개 변수를 보내는 나타납니다. 이 같은

뭔가 작업을해야합니다 : 다음 NAME1 및 NAME2에 액세스 할 수 있습니다

$json = json_decode($_POST['json']); 

을 : PHP는 측면에서 먼저 JSON을 디코딩 할 필요가 당신은 여전히 ​​오류를 얻을 경우

$name1 = $json['name1']; 
$name2 = $json['name2']; 

을, $ _POST 및 $ _GET 개체를 인쇄하고 데이터가 전송되는 방법을 확인하는 것이 좋습니다. 그런 다음 액세스하는 방법을 알게됩니다.


업데이트 :

당신이 array(0) { }을 받고있어 결과가 PHP 귀하의 요청에서 매개 변수를 (GET 또는 POST)하지 않았 음을 의미합니다

. 이처럼 다른 안드로이드 예를 시도 할 수 있습니다 :

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("http://example.com/JSON_FOLDER/JSON2/parseData.php"); 
post.setHeader("Content-type", "application/json"); 
post.setHeader("Accept", "application/json"); 

JSONObject json = new JSONObject(); 
json.put("name1", name1); 
json.put("name2", name2); 
post.setEntity(new StringEntity(json.toString(), "UTF-8")); 
HttpResponse response = client.execute(post); 

if(response != null) { 
    str = inputStreamToString(response.getEntity().getContent()).toString(); 
    Log.i("DATA", "Data send== " + str); 
} 

Reference Article

+0

$ _GET, $ _POST 그리고 'json', 'name1', 'name2'와 가능한 모든 조합을 시도했으며 항상 결과는 같습니다. 정의되지 않은 색인. 또한 'json'매개 변수가 앱에서 삭제되었지만 여전히 동일합니다. – t0s

+0

'var_dump ($ _ REQUEST);'를 실행하면 어떤 매개 변수가 PHP 코드를 작성하는지 알려줍니다. – Levi

+0

** 배열 (0) {} ** – t0s

관련 문제