android发送json数据到服务器(框)

package com.shoujifeng.design.http.app;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.Handler;
import android.widget.Toast;


import com.xxxx.design.beans.Famous_mit;
import com.xxxx.design.beans.Teacher_mit;
import com.xxxx.design.http.logic.HttpApp;
import com.xxxx.design.http.logic.HttpTimeoutListener;
import com.xxxx.design.http.logic.RespondType;
import com.xxxx.design.http.logic.HttpApp.AppOnRstListenerRegister;
import com.xxxx.design.param.SampleParam;

public class LoginRequset{
private Context mContext;
private HttpApp httpApp;
private String userName;
private String passWord;


public LoginRequset(){

}
//加载名师汇数据
public void Request(Context context,String type,String userName,String passWord) {

this.userName=userName;
this.passWord=passWord;

mContext = context;
ProgressDialogHint.Show(mContext, "提示", "正在登陆,请稍后...");
ParamUp mParamUp;
mParamUp = new ParamUp();

                 //设置每次请求时带上保存在客户端的cookies
mParamUp.cookieAction = ParamUp.COOKIE_ACTION_GET;    
//是属于什么接口
mParamUp.type = type;
mParamUp.contentType = ParamUp.CONTENT_TYPE_JSON;
try {
mParamUp.content = MakeJsonContentParam();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


// 启动超时
startTimeoutHandler();


httpApp = new HttpApp(mContext);
httpApp.AppRequestBegin(mParamUp, new AppOnRstListenerRegister() {
@Override
public int AppRstListenerRegister(ParamDown rst) {
// 停止超时
stopTimeoutHandler();
RequestHandle(rst);
return 0;
}
});
}
public void RequestCancel() {
if (httpApp != null)
httpApp.AppRequestCancel();
}


private String MakeJsonContentParam() throws JSONException{
JSONObject param = new JSONObject();
// 添加通用参数
if(userName!=null&&!equals(""))
{
param.put("userName", userName);
}

if(passWord!=null&&!equals(""))
{
param.put("password", passWord);
}

String jsonContent = param.toString();
return jsonContent;


}





private void RequestHandle(ParamDown rst) {
if (rst == null) {
if (mListener != null)
mListener.OnResultHandle(RespondType.RESULT_CODE_NULL,
RespondType.MESSAGE_NULL);
return;
}
String jsonResult = rst.result;
String Result=jsonResult.replace("\r\n\r\n\r\n", " ");
int code = RespondType.RESULT_CODE_NULL;
String msg = RespondType.MESSAGE_NULL;
// List<Famous_mit> famousMitsListviewData=null ;  //gridview 数据
try {
JSONObject jsonObj = new JSONObject(Result);
code = jsonObj.getInt(RespondType.RESULT_CODE);
msg = jsonObj.getString(RespondType.MESSAGE);
// list=jsonObj.getString(RespondType.LIST);
// JSONArray jsonArray = jsonObj.getJSONArray(RespondType.LIST);
// famousMitsListviewData = Famous_mit.constructArrayList(jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}



if (mListener != null)
mListener.OnResultHandle(code, msg);


}


public interface onLoadLoginListener {
public int OnResultHandle(int code, String msg);
}


private onLoadLoginListener mListener = null;


public void SetOnResultListener(onLoadLoginListener listener) {

mListener = listener;
}


// --------------------- 网络请求超时控制 -------------------------
private HttpTimeoutListener mTimeoutListener = null;


/*
* 设置超时回调 listener - 回调函数 timeoutDelay - 超时时间间隔(2000 = 2000ms)
*/
public void SetOnTimeoutListener(HttpTimeoutListener listener,
int timeoutDelay) {
mTimeoutListener = listener;
mTimetoutDelay = (timeoutDelay == -1) ? SampleParam.HTTP_DEFAULT_TIMEOUT
: timeoutDelay;
}


private int mTimetoutDelay = SampleParam.HTTP_DEFAULT_TIMEOUT; // 30秒
private Handler mTimeoutHandler = new Handler();
private Runnable mTimeoutRunnable = new Runnable() {
@Override
public void run() {
if (mTimeoutListener != null) {
// 取消网络请求
RequestCancel();
// 执行超时回调
mTimeoutListener.OnTimeoutHandle();
// 停止超时
stopTimeoutHandler();
}
}
};


/**
* 启动网络请求超时Handler
*/
private void startTimeoutHandler() {
if (mTimeoutHandler != null) {
mTimeoutHandler.removeCallbacks(mTimeoutRunnable);
mTimeoutHandler.postDelayed(mTimeoutRunnable, mTimetoutDelay);
}
}


/**
* 停止网络请求超时Handler
*/
private void stopTimeoutHandler() {
mTimeoutHandler.removeCallbacks(mTimeoutRunnable);
}
}




--------------------------------------------------------------------------------------------------------------------------




import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Set;


import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


import android.util.Log;


public class HttpPlatform {
// private static final String AES_KEY= "3566FC18AD03EC89"; // AES密钥


String TAG = "HttpPlatform";

private static CookieStore cookieStore = null; // cookie会话


private ParamDown mHttpDownContent = new ParamDown(); // 服务器下行返回参数



public ParamDown HttpPost(ParamUp paramUp) {
int cookieAction = paramUp.cookieAction;
String url = paramUp.type;
String content = paramUp.content;
int contentType = paramUp.contentType;
byte[] binaryData = paramUp.binaryData;


HttpPost httpPost = new HttpPost(url);
DefaultHttpClient httpClient = new DefaultHttpClient();



    Log.d(TAG, "-----------------HttpPost");
   
Log.d(TAG, url);
Log.d(TAG, content);

// 设置服务器当前会话cookie(即get出来的)
if (cookieAction == ParamUp.COOKIE_ACTION_SET) {
httpClient.setCookieStore(cookieStore);
}


                  //  contentType  是 将参数转成 json 格式
switch (contentType) {
case ParamUp.CONTENT_TYPE_JSON: // json 格式
try {
if (content != null) {
// 绑定到请求 Entry
StringEntity stringEntity = new StringEntity(content,      
HTTP.UTF_8);          
httpPost.setHeader("Content-Type",
"text/plain; charset=utf-8");
System.out.println("----"+stringEntity);
httpPost.setEntity(stringEntity);

}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
break;
case ParamUp.CONTENT_TYPE_BINARY: // 二进制数据
if (paramUp.mHeaderParamsMap != null
&& paramUp.mHeaderParamsMap.size() > 0) {
Set set = paramUp.mHeaderParamsMap.keySet();
for (Object key : set) {
httpPost.addHeader(key.toString(),
paramUp.mHeaderParamsMap.get(key));
}
}


// 图片数据
if (binaryData != null && binaryData.length > 0) {
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
binaryData);
byteArrayEntity.setContentType("application/octet-stream");
httpPost.setEntity(byteArrayEntity);
}
break;
default:
break;
}
// 发送请求并接收回来的参数
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
// 读取服务器返回的cookie
  if (cookieAction == ParamUp.COOKIE_ACTION_GET) {
cookieStore = httpClient.getCookieStore();
System.out.println("cookieStore:"+cookieStore.toString());
}
String respondEntityString = EntityUtils.toString(httpResponse
.getEntity());
System.out.println("----"+respondEntityString);
mHttpDownContent.result = respondEntityString;

Log.d(TAG, respondEntityString);

return mHttpDownContent;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


return null;
}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值