2013-04-07 59 views

回答

1

您需要提出http post请求。

http://developer.android.com/reference/org/apache/http/client/HttpClient.html

注意:如果您正在进行与网络相关的操作,则应该使用AsyncTask,否则您将得到一个NetworkOnMainThreadException(Honeycomb及更高版本)。

public void postData() { 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); 

try { 
    // Add your data 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 
    HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 
} 

一些源代码中的链接可低于

http://www.androidhive.info/2011/10/android-making-http-requests/

http://android-er.blogspot.in/2011/09/example-of-httppost-on-android.html

+0

但我强烈建议将方法主体放在AsyncTask中,并通过委托或LocalBroadcastManager消息将响应传递给调用者。 – ararog 2013-04-07 10:52:49

+1

@Raghunandan,所以为了将数据发送到网站,我确实需要看到我需要发送数据的密钥?这就是你如何在android中发送数据。所以,如果我正在开发一个其他网站的应用程序,我需要有php文件继续?如果我在这里错了,请纠正我,谢谢。 – 2013-04-07 10:54:01

+0

你能更清楚吗?所有你需要的是网站的网址和内容。 – Raghunandan 2013-04-07 10:56:51

0

由于您在WebView容器内部运行移动Web应用程序,因此可以使用像jQuery这样的众所周知的库及其ajax内容。

+1

,不使用Web容器,我能够从网站拉值。但我需要一个开始如何发送一些数据到网站。 – 2013-04-07 10:47:13

+0

请发表您迄今为止所尝试的内容,这将有助于提供更好的答案。 – ararog 2013-04-07 10:50:11

相关问题