登录状态校验
<pre><code class="language-java">package com.example.demo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
//这是需要改的,根据SDK选择不同地址
public static String url = &quot;http://www.***.com/sdk3.0/checkUsertoken.php&quot;; //登录校验地址(找运营获取域名地址)
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread (new Runnable() {
@Override
public void run() {
// 以下是示例代码
String str = getDemoJson();
InputStream is = doRequest(url, str);
String responseStr = readFromNetStream(is);
if (responseStr != null &amp;&amp; !&quot;&quot;.equals(responseStr)) {
handleResponse(responseStr);
} else {
// 连接 错误的代码
}
}
}).start();
}
/***
* 响应的处理,有可能是失败了,一般来说是成功的
* */
private void handleResponse(String responseStr) {
try {
JSONObject jo = new JSONObject(responseStr);
String status = jo.getString(&quot;status&quot;);
String msg = jo.getString(&quot;msg&quot;);
if (&quot;1&quot;.equals(status)) {// 表明响应成功
System.out.println(&quot;msg:&quot; + msg);
} else {// 响应失败
System.out.println(&quot;msg:&quot; + msg);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/***
* demoString 参照这个函数,接入人员自己设置自己的参数(不懂的和百度json格式的使用)
* { &quot;app_id&quot;:&quot;1&quot;, &quot;mem_id&quot;:&quot;23&quot;,
* &quot;user_token&quot;: &quot; rkmi2huqu9dv6750g5os11ilv2&quot;,
* &quot;sign&quot;:&quot;4753dce3ae736e7f894ebcc6cd3cffbc&quot; }
* */
public String getDemoJson() {
JSONObject jo = new JSONObject();
try {
jo.put(&quot;app_id&quot;, &quot;1&quot;);
jo.put(&quot;mem_id&quot;, &quot;23&quot;);
jo.put(&quot;user_token&quot;, &quot;rkmi2huqu9dv6750g5os11ilv2&quot;);
jo.put(&quot;sign&quot;, &quot;4753dce3ae736e7f894ebcc6cd3cffbc&quot;);
} catch (JSONException e) {
e.printStackTrace();
}
return jo.toString();
}
/**
* @param request
* 网络的输入流
* @不需要解压缩 zip 直接用json格式
*
* */
public String readFromNetStream(InputStream request) {
try {
byte[] buffer = new byte[1024];
int num = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (-1 != (num = request.read(buffer))) {
bos.write(buffer, 0, num);
}
return bos.toString(&quot;utf-8&quot;);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**************************************************************************************************************************/
/**
* 负责发送请求与请求后获取数据(加密压缩发送)
*
* @param url
* 请求地址
* @param str
* 请求携带的参数
* @return inputstream服务端返回的输入流
*/
public InputStream doRequest(String url, String str) {
HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30 * 1000);
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30 * 1000);
HttpPost post = new HttpPost(url);
post.setHeader(&quot;content-type&quot;, &quot;application/json&quot;);
if (str != null) {
HttpEntity entity = new ByteArrayEntity(str.getBytes());
post.setEntity(entity);
}
int count = 0;
// 等待3秒在请求2次
while (count &lt; 2) {
try {
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK) {
return response.getEntity().getContent();
}
} catch (ClientProtocolException e) {
System.out.println(&quot;网络异常&quot;);
e.printStackTrace();
} catch (IOException e) {
System.out.println(&quot;网络异常&quot;);
e.printStackTrace();
}
count++;
try {
// 请求失败,在请求一次
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
System.out.println(&quot;网络异常&quot;);
e.printStackTrace();
}
}
return null;
}
}
</code></pre>