问题
I'm very inexperienced when it comes to developing Android Applications and I cannot code a lot of Java. I have been google'ing around for around 2 hours now, trying many different examples of how to send POST data to a HTTP web server page and getting the outputted data, but none work. I am using the Android SDK Version 4.0 (API 14), does anyone know how to do this? Just a simple post some data, and get the output.
Thanks.
EDIT: Here is my current code,
package me.babblebox.application;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class BabbleBoxActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void check_login() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://babblebox.me/android/test.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
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
}
}
public void check_login_button(View v) {
check_login();
}
}
manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.babblebox.application"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" /><application android:icon="@drawable/bb_launch_icon" android:label="@string/app_name" android:testOnly="false" android:name=".BabbleBox" android:enabled="true">
<activity android:label="@string/app_name" android:name=".BabbleBoxActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Button XML that calls the method:
<Button
android:id="@+id/Button_Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="check_login_button"/>
回答1:
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", "Hi"));
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
}
}
Also you must add permissions to ApplicationManifest.xml to allow internet:
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
EDIT:
Your problem is not in the POST code, but how you are calling the onClick method, try this to fix your problem:
public void buttonListener(View v) {
check_login();
}
And call make your layout XML look similar to this:
<Button
android:text="Calling From XML Layout"
android:id="@+id/Button04" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener">
</Button>
You must add one parameter to Listener method that is the View.
来源:https://stackoverflow.com/questions/8464204/android-api-14-post-data-to-http