package com.example.model;
import com.example.bean.User;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class LoginModel {
public User LoginData(String name,String pwd){
String urls="http://www.zhaoapi.cn/user/login?mobile="+name+"&password="+pwd;
try {
URL url = new URL(urls);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
if (con.getResponseCode()==HttpURLConnection.HTTP_OK){
InputStream inputStream = con.getInputStream();
String string = getString(inputStream);
Gson gson = new Gson();
User user = gson.fromJson(string, User.class);
return user;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String getString(InputStream inputStream) throws IOException {
InputStreamReader reader = new InputStreamReader(inputStream);
StringBuffer buffer = new StringBuffer();
char[] chars = new char[1024];
int v=0;
while ((v=reader.read(chars))!=-1){
String s = new String(chars,0,v);
buffer.append(s);
}
return buffer.toString();
}
}