android接口 post,sdk - Android API 14 - POST Data to HTTP - Stack Overflow

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 nameValuePairs = new ArrayList(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:

package="me.babblebox.application"

android:versionCode="1"

android:versionName="1.0" >

Button XML that calls the method:

android:id="@+id/Button_Login"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Login"

android:onClick="check_login_button"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值