doumang20060820 2014-08-04 22:25
浏览 51
已采纳

使用android从php发送和接收数据

I have a php file, the class name is YoutubeToMp3.It has a function named as "get".It works truely.When i want to print it's result in server I do it like this echo YoutubeToMP3::get('https://www.youtube.com/watch?v=sEhy-RXkNo0', YoutubeToMP3::LINK); The problem is i want to send data from android to php in server and call the function "get" with using sent data from android and send back the "get" function's return data to android.For example in android i send data named url.As below

public void postData(String toPost) {
// Create a new HttpClient and Post Header

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://www.URL.com/yourpage.php");


//This is the data to send

String url = 'https://www.youtube.com/watch?v=sEhy-RXkNo0'; //any data to send



try {

// Add your data

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

nameValuePairs.add(new BasicNameValuePair("action", url));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);

//This is the response from a php application
String reverseString = response;
Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

} catch (ClientProtocolException e) {
Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}

}//end postData()

And i want to use function in server like this

YoutubeToMP3::get(url, YoutubeToMP3::LINK); 

Clearly i wanna tell you this: i sent data to server but in server how can i take this data and use it with get function and send back the data to android? Thanks ind advance. Any help would be appreciated. Note: i want to run this php files in wordpress.

  • 写回答

1条回答 默认 最新

  • dongsi3826 2014-08-04 22:43
    关注

    Use the $_POST global variable to read the data sent to this php script. So $_POST['action'] will contain the string value of variable action that you sent.

    EDIT:

    if($_POST['action']){ YoutubeToMP3::get($_POST['action'], YoutubeToMP3::LINK);}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog