Android的API 14 - POST数据到HTTP [英] Android API 14 - POST Data to HTTP

查看:198
本文介绍了Android的API 14 - POST数据到HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很没有经验,当涉及到开发Android应用程序,我不能code大量的Java。我一直在google'ing周围约2小时,试图如何POST数据发送到HTTP Web服务器页面,并获取输出的数据,但没有工作的许多不同的例子。我使用了Android SDK 4.0版(API 14),没有人知道如何做到这一点?只是一个简单的发布一些数据,并得到输出。

感谢。

编辑:这是我目前的code,

 包me.babblebox.application;进口java.io.IOException异常;
进口的java.util.ArrayList;
进口的java.util.List;进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;公共类BabbleBoxActivity延伸活动{
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }
    公共无效check_login(){
        //创建一个新的HttpClient和邮政头
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(http://babblebox.me/android/test.php);        尝试{
            //添加数据
            清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
            nameValuePairs.add(新BasicNameValuePair(ID,12345));
            nameValuePairs.add(新BasicNameValuePair(StringData是,你好));
            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));            //执行HTTP POST请求
            HTT presponse响应= httpclient.execute(httppost);        }赶上(ClientProtocolException E){
            // TODO自动生成catch块
        }赶上(IOException异常五){
            // TODO自动生成catch块
        }
    }
    公共无效check_login_button(视图v){
           check_login();
    }
}

清单文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=me.babblebox.application
    安卓版code =1
    机器人:=的versionName1.0>    <采用-SDK安卓的minSdkVersion =14/><应用机器人:图标=@绘制/ bb_launch_icon机器人:标签=@字符串/ APP_NAME机器人:testOnly =false的机器人:名字= .BabbleBox机器人:启用=真正的>
        <活动机器人:标签=@字符串/ APP_NAME机器人:名字=。BabbleBoxActivity>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
    <使用许可权的android:NAME =android.permission.INTERNET对/>< /清单>

按钮XML调用该方法:

 <按钮
        机器人:ID =@ + ID / Button_Login
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=登陆
        安卓的onClick =check_login_button/>


解决方案

 公共无效POSTDATA(){
    //创建一个新的HttpClient和邮政头
    HttpClient的HttpClient的=新DefaultHttpClient();
    HttpPost httppost =新HttpPost(http://www.yoursite.com/script.php);    尝试{
        //添加数据
        清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
        nameValuePairs.add(新BasicNameValuePair(ID,12345));
        nameValuePairs.add(新BasicNameValuePair(StringData是,你好));
        httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));        //执行HTTP POST请求
        HTT presponse响应= httpclient.execute(httppost);    }赶上(ClientProtocolException E){
        // TODO自动生成catch块
    }赶上(IOException异常五){
        // TODO自动生成catch块
    }
}

另外你必须添加权限ApplicationManifest.xml让互联网:

 <清单xlmns:安卓...>
 ...
 <使用许可权的android:NAME =android.permission.INTERNET对>< /使用许可权>
< /清单>

编辑:

您的问题不是在POST code,但你是如何调用的onClick方法,试试这个解决您的问题:

 公共无效buttonListener(视图v){
   check_login();
}

和打电话让你的布局XML类似于此:

 <按钮
   机器人:文字=调用的XML布局
   机器人:ID =@ + ID / Button04的android:layout_width =FILL_PARENT
   机器人:layout_height =WRAP_CONTENT
   安卓的onClick =buttonListener>
< /按钮>

您必须在一个参数添加到监听器方法是查看。

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"/>

解决方案

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.

这篇关于Android的API 14 - POST数据到HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆