package com.chengdong.su.util.get;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.widget.Toast;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
* 异步请求服务器
*
* @author scd
*
*/
public class AsyncJsonUtilGet {
private static final String URL = "";
private Context mContext;
/**
* 构造方法
*
* @param mContext
*/
public AsyncJsonUtilGet(Context mContext) {
super();
this.mContext = mContext;
}
/**
* 邮箱注册
*/
public void emailRegister(String email, String password, String username) {
RequestParams params = new RequestParams();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("email", email);
jsonObject.put("password", password);
jsonObject.put("username", username);
} catch (JSONException e) {
e.printStackTrace();
}
params.put("jsonObject", jsonObject);
// get请求方式
HttpUtil.get(URL, params, new JsonHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers,
Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Toast.makeText(mContext, "Register failed!", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
String errorCode = response.optString("ErrorCode");
// 表示请求成功
if (errorCode.equals("0")) {
Toast.makeText(mContext, "注册成功", Toast.LENGTH_LONG).show();
// response:返回的数据都在这个参数中,根据业务要求进行实现功能
} else {
super.onSuccess(statusCode, headers, response);
}
}
});
}
}
package com.chengdong.su.util.get;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class HttpUtil {
public static final String STATUS_NETWORK = "network_available";
private static AsyncHttpClient client = new AsyncHttpClient();
static {
client.setTimeout(11000);
}
public static void get(String urlString, AsyncHttpResponseHandler res) {
client.get(urlString, res);
}
public static void get(String urlString, RequestParams params,
AsyncHttpResponseHandler res) {
client.get(urlString, params, res);
}
public static void get(String urlString, JsonHttpResponseHandler res)
{
client.get(urlString, res);
}
public static void get(String urlString, RequestParams params,
JsonHttpResponseHandler res)
{
client.get(urlString, params, res);
}
public static void get(String uString, BinaryHttpResponseHandler bHandler)
{
client.get(uString, bHandler);
}
public static AsyncHttpClient getClient() {
return client;
}
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
// 从UrlConnection中获取文件名称
public static String getFileName(String url) {
String fileName = null;
boolean isOK = false;
try {
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
if (conn == null) {
return null;
}
Map<String, List<String>> hf = conn.getHeaderFields();
if (hf == null) {
return null;
}
Set<String> key = hf.keySet();
if (key == null) {
return null;
}
for (String skey : key) {
List<String> values = hf.get(skey);
for (String value : values) {
String result;
try {
result = value;
int location = result.indexOf("filename");
if (location >= 0) {
result = result.substring(location
+ "filename".length());
result = java.net.URLDecoder
.decode(result, "utf-8");
result = result.substring(result.indexOf("\"") + 1,
result.lastIndexOf("\""));
fileName = result
.substring(result.indexOf("=") + 1);
isOK = true;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
if (isOK) {
break;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有