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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程