android 向服务器Get和Post请求的两种方式,android向服务器发送文件,自己组装协议和借助第三方开源

一个适用于Android平台的第三方和apache的很多东西类似,只是用于Android上

我在项目里用的是这个

https://github.com/loopj/android-async-http

 AsyncHttpClient  client = new AsyncHttpClient();


RequestParams params = new RequestParams();
params.put(JsonKey.JSON_K_MOBILE, "1526808");
params.put(JsonKey.JSON_K_COUPON, coupon);

put(File,...);


/** 
 * @author intbird@163.com 
 * @time 20140606 
 */ 
package com.intbird.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

public class ConnInternet {
	/**
	 * 开始连接
	 */
	public static int HTTP_STATUS_START=0;
	/**
	 * 连接过程出错
	 */
	public static int HTTP_STATUS_ERROR=400;
	/**
	 * 请求成功,返回数据
	 */
	public static int HTTP_STATUS_REULTOK=200;
	/**
	 * 服务器未响应
	 */
	public static int HTTP_STATUS_NORESP=500;
	
	/**
	 * 普通GET请求
	 * @param api
	 * @param callBack
	 */
	public static void get1(final String api,final ConnInternetCallback callBack) {
		final Handler handler=getHandle(api, callBack);
		new Thread(){
			@Override
			public void run() {
				Message msg=handler.obtainMessage(HTTP_STATUS_START, "开始连接");
				try {
					URL url = new URL(api);
					HttpURLConnection urlConn =  (HttpURLConnection) url.openConnection();
					int resCode=urlConn.getResponseCode();
					if(resCode!=HTTP_STATUS_REULTOK){
						msg.what=HTTP_STATUS_NORESP;
						msg.obj=resCode;
						handler.sendMessage(msg);
						return ;
					}
					InputStreamReader inStream=new InputStreamReader(urlConn.getInputStream());
					BufferedReader buffer=new BufferedReader(inStream);
					
					String result="";
					String inputLine=null;
					while((inputLine=buffer.readLine())!=null){
						result+=inputLine;
					}
					msg.what=HTTP_STATUS_REULTOK;
				    msg.obj=URLDecoder.decode(result,"UTF-8");
				    
					inStrea
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值