public class NewsUtils {
private static Context context;
public static String call(Context mContext,String urlName,String methodName,String keyName){
context=mContext;
String NewUrl=urlName;
if(methodName=="GET"){
NewUrl=urlName+"?"+keyName;
}
StringBuffer result=new StringBuffer();
try {
URL url=new URL(NewUrl);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod(methodName);
if(methodName.equals("POST")){
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dataOutputStream=new
DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(keyName);
dataOutputStream.flush();
dataOutputStream.close();
}
connection.connect();
if(HttpURLConnection.HTTP_OK==connection.getResponseCode()){
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while((line=bufferedReader.readLine())!=null){
result.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
}
private static Context context;
public static String call(Context mContext,String urlName,String methodName,String keyName){
context=mContext;
String NewUrl=urlName;
if(methodName=="GET"){
NewUrl=urlName+"?"+keyName;
}
StringBuffer result=new StringBuffer();
try {
URL url=new URL(NewUrl);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod(methodName);
if(methodName.equals("POST")){
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream dataOutputStream=new
DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(keyName);
dataOutputStream.flush();
dataOutputStream.close();
}
connection.connect();
if(HttpURLConnection.HTTP_OK==connection.getResponseCode()){
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while((line=bufferedReader.readLine())!=null){
result.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
}