2012-11-07 1 views
0

cakephp로 만든 웹 사이트가 있습니다. 내 앱에서 형성된 일부 가치를이 웹 사이트에 전달하고 싶습니다. 브라우저에 정확히 동일한 URL을 입력하면 작동합니다. www.something.com/function/add/valueAndroid HTTP CakePHP URL 요청

그래서 나는 이것이 GET이나 POST 방식의 경우 매우 혼란 스러워요 :

URL은 같은입니까? 내가 어떻게 할 수 있니?

나는이 URL을 변경할 수 없거나 POST 또는 GET PHP 스크립트를 사용하여 값을 가져올 수 없다는 것을 알고 있습니다. 그래서 기본적으로 그 매개 변수로 URL을 호출해야합니다.

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = null; 
try { 
     httppost = new HttpPost("www.something.com/function/add/" + URLEncoder.encode(txtMessage.getText().toString(), "UTF-8")); 
} catch (UnsupportedEncodingException e1) { 
     e1.printStackTrace(); 
} 

try { 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     httpclient.execute(httppost, responseHandler); 
} catch (ClientProtocolException e) { 
} catch (IOException e) { 
} 

답변

1

List<NameValuePair>를 만들고 (예에서 "someValue와")을 당신에게 여기에 값을 넣어 :

이 내 코드입니다. DefaultHttpClient()nameValuePairs으로 설정하고 값을 입력하십시오. /function/add/utils.php에서 서버 측에

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("tag", "TEST_TAG")); 
nameValuePairs.add(new BasicNameValuePair("valueKey", "somevalue")); 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("www.something.com/function/add/utils.php"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
StringBuilder sb = new StringBuilder(); 
String line = ""; 
while ((line = reader.readLine()) != null) { 
    sb.append(line + "n"); 
} 
is.close(); 
Log.i("response", sb.toString()); 

은 당신이 당신이 HttpEntity entity = response.getEntity() 같은 자바 코드에서 받아 봐이 $response

if (isset($_POST['tag']) && $_POST['tag'] != '') { 
    $tag = $_POST['tag']; //=TEST_TAG 
    $value = $_POST['valueKey']; //=somevalue 
} 
//and return some info 
$response = array("tag" => $tag, "success" => 0, "error" => 0); 
$response["success"] = 1; 
echo json_encode($response); 

가치를 얻을. 그것은 당신을 도울 수 있습니다.

+0

글쎄요, 미안 해요. 내 URL에는 PHP 파일 등이 포함되어 있지 않습니다. 그냥 일반 URL (CakePHP가 생성하는 방식 ...) 서버는 URL에서 직접 값을 읽습니다. 그것을하는 다른 방법이 있습니까? – user754730

+0

'www.something.com/function/add /'와 같은 링크가 있습니다. 서버에서 handler-file을 찾으십시오. 이 디렉토리에서'function/add /'는'index.php'이어야합니다. 거기 있니? – validcat

+0

아니요 PHP 파일이없고 아무 것도 없을 것입니다. www.something.com/function/add/myvaluetobeadded 링크는 그렇게합니다. 그런 다음 CakePHP는 "myvaluetobeadded"가 웹 사이트에 제공하는 실제 값이라는 것을 알고 있습니다. 브라우저로 웹 사이트를 탐색하면 멋지게 작동합니다. – user754730