Android的HTTP请求的URL的CakePHP [英] Android HTTP Request for CakePHP URL

查看:170
本文介绍了Android的HTTP请求的URL的CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被使用CakePHP创建了一个网站。我想通过形成在我的应用程序本网站中的值。当我在它的工作原理浏览器中输入网址完全相同。

的URL是这样的:www.something.com/function/add/value

所以我很困惑,如果这是一个GET或POST方法?我该怎么办呢?

问题是,我不能改变这个URL,或放一些POST或GET PHP脚本那里得到的值。所以我基本上只需要调用这些PARAMS的URL。

这是我的code:

 的HttpClient HttpClient的=新DefaultHttpClient();
HttpPost httppost = NULL;
尝试{
        httppost =新HttpPost(www.something.com/function/add/+ URLEn coder.en code(txtMessage.getText()的toString(),UTF-8));
}赶上(UnsupportedEncodingException E1){
        e1.printStackTrace();
}尝试{
        ResponseHandler所<串GT; ResponseHandler所=新BasicResponseHandler();
        httpclient.execute(httppost,ResponseHandler所);
}赶上(ClientProtocolException E){
}赶上(IOException异常五){
}


解决方案

创建列表<的NameValuePair> ,并把这里你值(例如,在someValue中)。创建 DefaultHttpClient()设置 namevaluepairs中与你的价值。

 列表<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(标签,TEST_TAG));
nameValuePairs.add(新BasicNameValuePair(valueKey,someValue中));
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(www.something.com/function/add/utils.php);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中,HTTP.UTF_8));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
InputStream为= entity.getContent();
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线=;
而((行= reader.readLine())!= NULL){
    sb.append(线+的n);
}
is.close();
Log.i(回应,sb.toString());

在中 /function/add/utils.php 服务器端得到你的重视。

 如果(使用isset($ _ POST ['标签'])及和放大器;!$ _ POST ['标签'] =''){
     $标签= $ _ POST ['标签']; // = TEST_TAG
     $值= $ _ POST ['valueKey']; // = someValue中
}
//并返回一些信息
$响应=阵列(标记=> $标签,成功=大于0,错误=大于0);
$响应[成功] = 1;
回声json_en code($响应);

$回应你在你的java code recive为 HttpEntity实体= response.getEntity() 。它可以帮助你。

I have a website which was created with cakephp. I want to pass some values formed in my App to this website. When I enter the exact same URL in the browser it works.

The URL is something like: www.something.com/function/add/value

So I'm very confused if this is a GET or a POST method? And how I can do that?

The thing is that I can NOT change this URL or put some POST or GET PHP script there to get the values. So I basically just have to call the URL with those params.

This is my code:

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) {
}

解决方案

Create List<NameValuePair> and put here yours values ("somevalue" in example). Create DefaultHttpClient() set nameValuePairs with yours value.

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

On the server side in /function/add/utils.php get yours value

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);

This $response you recive in your java code as HttpEntity entity = response.getEntity(). It may help you.

这篇关于Android的HTTP请求的URL的CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆