80,351
社区成员
发帖
与我相关
我的任务
分享
//String url = "http://101.200.201.132/webapp/user/uploadsport";
//对于你这个接口,应该类似这样:
String url = "http://ip地址/GetNewsInfo?account=123&pwd=123&newstype=4&showArea=1&userkey=123&valiatedcode=123";
String json = NetWorkUtils.getJsonFromUrl(url);
//这个函数发送对应请求,并读取返回的结果,根据你提供的接口,返回的结果应该也是json格式的
public static String getJsonFromUrl(String url){
String jason = "";
URL excelUrl=null;
try
{
excelUrl=new URL(url);
}
catch (MalformedURLException e)
{
Log.e(TAG, "getJsonFromUrl err:"+e.getMessage());
}
HttpURLConnection urlConn= null;
try {
Log.e(TAG, "getJsonFromUrl openConnection...");
urlConn = (HttpURLConnection)excelUrl.openConnection();
urlConn.setConnectTimeout(10000);
// urlConn.setUseCaches(false);
urlConn.setRequestProperty("Charset", "UTF-8");
urlConn.setRequestMethod("GET");
// urlConn.setDoOutput(true);
Log.e(TAG, "getJsonFromUrl getResponseCode...");
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
Log.e(TAG, "getJsonFromUrl getResponseCode HTTP_OK");
InputStream input = urlConn.getInputStream();
byte buffer [] = new byte[1024];
int inputSize = -1;
while((inputSize = input.read(buffer)) != -1) {
jason += new String(buffer);//读取返回的结果内容
}
Log.e(TAG, "getJsonFromUrl ok!");
}else{
Log.e(TAG, "getJsonFromUrl getResponseCode error!");
}
} catch (Exception e) {
// e.printStackTrace();
Log.e(TAG, "getJsonFromUrl Exception:"+e.getMessage());
}
return jason;
}