打开modie下的buil.gradle文件中的android的根标签,添加
红框中则是要添加的代码;
代码如下:
public static String getClient(String urlString) {
//创建DefaultHttpClient
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//两种请求方式
HttpGet httpGet = new HttpGet(urlString);
//HttpPost httpPost = new HttpPost(urlString);
try {
//执行并得到服务器返回的数据
HttpResponse response = defaultHttpClient.execute(httpGet);
//得到状态码
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200){
//用response获取类返回数据
HttpEntity entity = response.getEntity();
//string就是json的字符串,剩下的则是解析工作
String string = EntityUtils.toString(entity);
return string;
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}