网易云信课题实践-注册请求处理
作者:互联网
这里写自定义目录标题
注册发生空指针异常
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/user/create.action";
HttpPost httpPost = new HttpPost(url);
String appKey = "17c6c55a5de3cd2245baa6df790abd40";
String appSecret = "db6e15894817";
String nonce = "12345";
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", appKey);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", "helloworld"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 执行请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
// 打印执行结果
try {
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
一开始一直出现空指针异常,起初我以为是因为网络问题或者是response为空,其实是在java中不能以主线程来发送请求,会导致程序卡住,要创建新的进程,处理办法如下
import android.util.Log;
import com.example.app111.CheckSumBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Post1 implements Runnable{
private Thread thread;
private String tName;
Post1(String tName){
this.tName=tName;
}
@Override
public void run() {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/user/create.action";
HttpPost httpPost = new HttpPost(url);
String appKey = "17c6c55a5de3cd2245baa6df790abd40";
String appSecret = "db6e15894817";
String nonce = "12345";
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", appKey);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", "helloworld"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 执行请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
// 打印执行结果
try {
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(){
if(thread==null){
thread=new Thread(this,tName);
thread.start();
}
}
}
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
reg=findViewById(R.id.register);
Thread thread=new Thread();
Post1 post = new Post1("p");
post.start();
标签:课题,网易,java,String,addHeader,云信,httpPost,import,new 来源: https://blog.csdn.net/erefas/article/details/115266146