Android 4.0 链接本地iis 出错 求高手帮忙
iis服务器里面是用.net写的ashx文件,用于返回json。
在浏览器中访问http://192.168.1.20:805/news.ashx?ntid=2&startpage=0&count=10 是可以正常返回的。客户端中:出错代码如下:
这个是在Activity中的方法
private List<HashMap<String, Object>> getNewsList(int num)
{
List<HashMap<String, Object>> newslist = new ArrayList<HashMap<String, Object>>();
SyncHttp http = new SyncHttp();
String urlStr = "http://192.168.1.20:805/news.ashx";
String paramsStr = "ntid=" + num + "&startpage=" + NewsPage + "&count=" + NewsCount;
try
{
String retstr = http.httpGet(urlStr, paramsStr);// 访问服务器返回json
JSONObject jsonObject = new JSONObject(retstr);// 设置为json对象
String success = jsonObject.getString("success");// 获取节点是否成功
if (success == "0")
{
JSONObject dataobj = jsonObject.getJSONObject("data");// 获取数据节点
int newsnum = jsonObject.getInt("count");// 获取返回的总数
if (newsnum > 0)
{
JSONArray array = dataobj.getJSONArray("table");// 获取新闻list
for (int i = 0; i < array.length(); i++)
{
JSONObject newsobj = (JSONObject) array.opt(i);
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("NID", newsobj.getString("NID"));
hm.put("list_img", R.drawable.ic_launcher);
hm.put("list_title", newsobj.getString("NTitle"));
hm.put("list_address", "来源:" + newsobj.getString("NSource"));
hm.put("list_tel", "点击率" + newsobj.getInt("NHis"));
newslist.add(hm);
}
} else
{
Toast.makeText(listActivity.this, "获取新闻失败", Toast.LENGTH_LONG).show();
} } else
{
Toast.makeText(listActivity.this, "获取新闻失败", Toast.LENGTH_LONG).show();
} } catch (Exception e)
{
Toast.makeText(listActivity.this, "获取新闻失败"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return newslist;
}
SyncHttp类private static final int TIME_OUT = 1000 * 6; // 超时
private static final String METHOD_POST = "POST";
private static final String METHOD_GET = "GET";
private static final int HTTP_OK = 200;
private final String CHARTSET = "UTF-8"; // 字符编码
private final int BUFFER = 1024 * 8;// 缓冲区 public String httpGet(String urlStr, String paramsStr) throws Exception
{
// 构造请求的URL
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(urlStr);
if (!TextUtil.isEmpty(paramsStr))
{
urlBuilder.append("?");
urlBuilder.append(paramsStr);
}
// Log.i(Constants.TAG, urlBuilder.toString());
URL url = null;
HttpURLConnection conn = null;
InputStream inStream = null;
String response = null;
try
{
url = new URL(urlBuilder.toString());
//System.out.println("");
// 根据URL打开远程连接
conn = (HttpURLConnection) url.openConnection();
// 设置参数
conn.setDoInput(true);
conn.setConnectTimeout(TIME_OUT);
conn.setRequestMethod(METHOD_GET);
conn.setRequestProperty("accept", "*/*");
// 建立连接
conn.connect();
// 接受返回码
int responseCode = conn.getResponseCode();
if (responseCode == HTTP_OK)
{
// 获取输入流
inStream = conn.getInputStream();
// 从输入流中获取信息
response = getResponse(inStream);
} else
{
// 请求失败
response = "返回码:" + responseCode;
}
} catch (Exception e)
{
throw e;
} finally
{
// 关闭连接
conn.disconnect();
}
return response;
}我调用的是httpget方法
错误是在:conn.connect(); 出错的
在Manifest中配置有:<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
log是 黄色的 07-23 16:17:37.267: W/System.err(3789): android.os.NetworkOnMainThreadException
07-23 16:17:37.277: W/System.err(3789):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
07-23 16:17:37.307: D/dalvikvm(3789): GC_CONCURRENT freed 41K, 2% free 7881K/8007K, paused 4ms+5ms
07-23 16:17:37.307: W/System.err(3789):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74)
07-23 16:17:37.307: W/System.err(3789):  at libcore.io.IoBridge.connectErrno(IoBridge.java:138)
07-23 16:17:37.307: W/System.err(3789):  at libcore.io.IoBridge.connect(IoBridge.java:112)
07-23 16:17:37.307: W/System.err(3789):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-23 16:17:37.307: W/System.err(3789):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-23 16:17:37.327: W/System.err(3789):  at java.net.Socket.connect(Socket.java:842)
07-23 16:17:37.327: W/System.err(3789):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
07-23 16:17:37.336: W/System.err(3789):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-23 16:17:37.336: W/System.err(3789):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
07-23 16:17:37.336: W/System.err(3789):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
07-23 16:17:37.336: W/System.err(3789):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-23 16:17:37.336: W/System.err(3789):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
07-23 16:17:37.336: W/System.err(3789):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
07-23 16:17:37.336: W/System.err(3789):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
07-23 16:17:37.346: W/System.err(3789):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
07-23 16:17:37.346: W/System.err(3789):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
07-23 16:17:37.346: W/System.err(3789):  at com.hy.woyounews.service.SyncHttp.httpGet(SyncHttp.java:63)
07-23 16:17:37.346: W/System.err(3789):  at com.hy.woyounews.activity.listActivity.getNewsList(listActivity.java:269)
07-23 16:17:37.346: W/System.err(3789):  at com.hy.woyounews.activity.listActivity.onCreate(listActivity.java:147)
07-23 16:17:37.357: W/System.err(3789):  at android.app.Activity.performCreate(Activity.java:4465)
07-23 16:17:37.357: W/System.err(3789):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-23 16:17:37.367: W/System.err(3789):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-23 16:17:37.367: W/System.err(3789):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-23 16:17:37.367: W/System.err(3789):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-23 16:17:37.367: W/System.err(3789):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-23 16:17:37.367: W/System.err(3789):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 16:17:37.376: W/System.err(3789):  at android.os.Looper.loop(Looper.java:137)
07-23 16:17:37.376: W/System.err(3789):  at android.app.ActivityThread.main(ActivityThread.java:4340)
07-23 16:17:37.376: W/System.err(3789):  at java.lang.reflect.Method.invokeNative(Native Method)
07-23 16:17:37.376: W/System.err(3789):  at java.lang.reflect.Method.invoke(Method.java:511)
07-23 16:17:37.386: W/System.err(3789):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-23 16:17:37.386: W/System.err(3789):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-23 16:17:37.386: W/System.err(3789):  at dalvik.system.NativeStart.main(Native Method)
07-23 16:17:37.756: W/TextLayoutCache(3789): computeValuesWithHarfbuzz -- need to force to single run
我百度了一下 有的说需要加下面的代码就可以了 但是还是不行StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads()
        .detectDiskWrites()
        .detectNetwork()   // or .detectAll() for all detectable problems
        .penaltyLog()
        .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
        .detectLeakedSqlLiteObjects()
        .detectLeakedClosableObjects()
        .penaltyLog()
        .penaltyDeath()
        .build());加上这个还是会报错 求解。 本人刚学这个,很多东西还不了解,求高手帮忙解决