美文网首页
android 与服务器http数据对接

android 与服务器http数据对接

作者: MakerSir | 来源:发表于2020-10-28 16:58 被阅读0次

public String networker_data(String url)

{

String result ="";

// String

    URL realURL =null;

URLConnection conn =null;

BufferedReader bufReader =null;

String line ="";

try {

realURL =new URL(url);

}catch (MalformedURLException e) {

// TODO Auto-generated catch block

        e.printStackTrace();

System.out.println("url 格式错误");

}

try {

//  String androidid=Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID);

        Log.d("***","http url: "+url);

conn = realURL.openConnection();

// 设置连接参数...conn.setRequestProperty("xx", "xx");

//  conn.setRequestProperty("mac", getMacAddress());

        conn.setConnectTimeout(10000);// 10s timeout

// conn.setRequestProperty("accept", "*/*");

// conn.setRequestProperty("", "");

        conn.connect();// 连接就把参数送出去了 get方法

    }catch (IOException e) {

// TODO Auto-generated catch block

        e.printStackTrace();

System.out.println("连接错误");

}

try {

bufReader =new BufferedReader(new InputStreamReader(  conn.getInputStream(),"utf-8"));

while ((line = bufReader.readLine()) !=null) {

result += line +"\n";

}

}catch (IOException e) {

// TODO Auto-generated catch block

        e.printStackTrace();

System.out.println("读取数据错误");

}finally {

// 释放资源

        if (bufReader !=null) {

try {

bufReader.close();

}catch (IOException e) {

// TODO Auto-generated catch block

                e.printStackTrace();

}

}

}

Log.d("***","http result: "+result);

return result;

}

相关文章

网友评论

      本文标题:android 与服务器http数据对接

      本文链接:https://www.haomeiwen.com/subject/qjlcvktx.html