关闭

android_31_开源AsyncHttpClient

标签: androidAsyncHttpClientGETPOST
95人阅读 评论(0) 收藏 举报
分类:

效果图:



将com文件夹复制到src目录,

其中,buildConfig可能报错, 要删除原来导入的buildConfig

重新导入buildConfig



清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sg31.asynchttpclient"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    // 注意权限 
    <uses-permission android:name="android.permission.INTERNET"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>






布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sg31.httpclient.MainActivity" 
    android:orientation="vertical"
    >

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入意见或建议" 
        android:text="啊,发现一个大Bug!!!"
        />
    <EditText
        android:id="@+id/et_contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请留下妳的联系方式(可空)" 
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getBtnClicked"
        android:text="GET方式提交"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="postBtnClicked"
        android:text="POST方式提交"
        />

    <ScrollView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    <TextView 
        android:id="@+id/tv_response"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</ScrollView>

</LinearLayout>



代码:


package com.sg31.asynchttpclient;

import java.net.URLEncoder;

import org.apache.http.Header;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void getBtnClicked(View v) {
		EditText et_content = (EditText) findViewById(R.id.et_content);
		EditText et_contact = (EditText) findViewById(R.id.et_contact);

		final String content = et_content.getText().toString();
		final String contact = et_contact.getText().toString();
		String url = "http://sg31.com/ci/androidfeedback/insertwithget?language=android&content="
						+ URLEncoder.encode(content) + "&contact=" + contact;
		// 创建异步httpclient
		AsyncHttpClient client = new AsyncHttpClient();

		// 发送get请求提交数据
		client.get(url, new SGResponseHandler());
	}

	public void postBtnClicked(View v) {
		EditText et_content = (EditText) findViewById(R.id.et_content);
		EditText et_contact = (EditText) findViewById(R.id.et_contact);

		final String content = et_content.getText().toString();
		final String contact = et_contact.getText().toString();
		String url = "http://sg31.com/ci/androidfeedback/insertwithpost";

		// 创建异步httpclient
		AsyncHttpClient client = new AsyncHttpClient();

		// 发送post请求提交数据
		// 把要提交的数据封装至RequestParams对象
		RequestParams params = new RequestParams();
		params.add("language", "android");
		params.add("content", content);
		params.add("contact", contact);
		client.post(url, params, new SGResponseHandler());
	}

	class SGResponseHandler extends AsyncHttpResponseHandler {

		// 请求服务器成功时,此方法调用
		
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {
			String result = new String(responseBody);
			System.out.println("sg__"+result);
			Toast.makeText(MainActivity.this, result, 0)
					.show();
			TextView tv_response = (TextView) findViewById(R.id.tv_response);
			tv_response.setText(result);

		}

		// 请求失败此方法调用
		
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {
			Toast.makeText(MainActivity.this, "请求失败", 0).show();
			

		}


	}

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:359330次
    • 积分:8356
    • 等级:
    • 排名:第2340名
    • 原创:472篇
    • 转载:2篇
    • 译文:0篇
    • 评论:75条
    最新评论