还有就是我用移动2 / 3G的手机浏览器访问我的接口没有问题,但是用自己开发的APP就出现问题了,这样是不是可以肯定不是接口问题,问题出现在代码。public List<Api_Model> getNewsFromJson(String Thephrase) throws Exception
{
List<Api_Model> lst = new ArrayList<Api_Model>();
String pathUTF8 = URLEncoder.encode(Thephrase,"utf-8");
String path = "http://qq408347877.vicp.cc:88/API/1.0/Json/Distinguish/Diatinguish/"+pathUTF8;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(50000);
conn.setRequestMethod("GET");
if (200 == conn.getResponseCode())
{
InputStream instream = conn.getInputStream();
lst = parseJSON(instream);
//instream.close();
}
return lst;
} private List<Api_Model> parseJSON(InputStream instream) throws Exception
{
List<Api_Model> list = new ArrayList<Api_Model>();
byte[] data = read(instream);
String jsonStr = new String(data);
String sr = jsonStr.substring(0,jsonStr.indexOf("]")+1);
String str = sr.substring(jsonStr.indexOf("["),sr.length());
JSONArray array = new JSONArray(str);
for (int i = 0; i < array.length(); i++)
{
JSONObject jsonObject = array.getJSONObject(i);
// 初始化map数组对象
Api_Model ms = new Api_Model(
jsonObject.getString("Departure"),
jsonObject.getString("Destination"),
jsonObject.getString("Method"),
jsonObject.getString("Search"),
jsonObject.getString("Message"),
jsonObject.getString("JumpStart"));
list.add(ms);
}
return list;
}
public static byte[] read(InputStream instream) throws IOException
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = instream.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
return bos.toByteArray();
}
{
List<Api_Model> lst = new ArrayList<Api_Model>();
String pathUTF8 = URLEncoder.encode(Thephrase,"utf-8");
String path = "http://qq408347877.vicp.cc:88/API/1.0/Json/Distinguish/Diatinguish/"+pathUTF8;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(50000);
conn.setRequestMethod("GET");
if (200 == conn.getResponseCode())
{
InputStream instream = conn.getInputStream();
lst = parseJSON(instream);
//instream.close();
}
return lst;
} private List<Api_Model> parseJSON(InputStream instream) throws Exception
{
List<Api_Model> list = new ArrayList<Api_Model>();
byte[] data = read(instream);
String jsonStr = new String(data);
String sr = jsonStr.substring(0,jsonStr.indexOf("]")+1);
String str = sr.substring(jsonStr.indexOf("["),sr.length());
JSONArray array = new JSONArray(str);
for (int i = 0; i < array.length(); i++)
{
JSONObject jsonObject = array.getJSONObject(i);
// 初始化map数组对象
Api_Model ms = new Api_Model(
jsonObject.getString("Departure"),
jsonObject.getString("Destination"),
jsonObject.getString("Method"),
jsonObject.getString("Search"),
jsonObject.getString("Message"),
jsonObject.getString("JumpStart"));
list.add(ms);
}
return list;
}
public static byte[] read(InputStream instream) throws IOException
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = instream.read(buffer)) != -1)
{
bos.write(buffer, 0, len);
}
return bos.toByteArray();
}
if (200 == conn.getResponseCode())
{
InputStream instream = conn.getInputStream();
System.out.println(instream.toString());
lst = parseJSON(instream);
//instream.close();
conn.disconnect();
}
从红色哪里出现异常异常信息mw_shl_code=java,true]07-22 23:18:09.328: W/ActivityThread(15211): Application com.example.nslocatesystem is waiting for the debugger on port 8100...
07-22 23:18:12.258: W/ResponseProcessCookies(15211): Invalid cookie header: "Set-Cookie: BAIDUID=158F8C983C6A15F2B0E32B7CCB0085E0:FG=1; max-age=31536000; expires=Wed, 17-Dec-14 05:50:28 GMT; domain=.baidu.com; path=/; version=1". Unable to parse expires attribute: Wed
07-22 23:18:43.668: W/System.err(15211): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
07-22 23:18:43.668: W/System.err(15211): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:542)
07-22 23:18:43.668: W/System.err(15211): at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
07-22 23:18:43.668: W/System.err(15211): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
07-22 23:18:43.668: W/System.err(15211): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
07-22 23:18:43.668: W/System.err(15211): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
07-22 23:18:43.668: W/System.err(15211): at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
07-22 23:18:43.668: W/System.err(15211): at libcore.net.http.UnknownLengthHttpInputStream.read(UnknownLengthHttpInputStream.java:41)
07-22 23:18:43.668: W/System.err(15211): at java.io.InputStream.read(InputStream.java:163)
07-22 23:18:43.668: W/System.err(15211): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
07-22 23:18:43.668: W/System.err(15211): at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
07-22 23:18:43.668: W/System.err(15211): at java.io.InputStreamReader.read(InputStreamReader.java:244)
07-22 23:18:43.668: W/System.err(15211): at org.kxml2.io.KXmlParser.fillBuffer(KXmlParser.java:1496)
07-22 23:18:43.678: W/System.err(15211): at org.kxml2.io.KXmlParser.readValue(KXmlParser.java:1340)
07-22 23:18:43.688: W/System.err(15211): at org.kxml2.io.KXmlParser.next(KXmlParser.java:390)
07-22 23:18:43.688: W/System.err(15211): at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:241)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:151)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:253)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
07-22 23:18:43.688: W/System.err(15211): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
07-22 23:18:43.698: W/System.err(15211): at com.example.activity.BaseActivity.getRemoteInfo(BaseActivity.java:349)
07-22 23:18:43.698: W/System.err(15211): at com.example.map.activity.WarningMap$8.run(WarningMap.java:247)
07-22 23:18:43.698: W/System.err(15211): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
07-22 23:18:43.698: W/System.err(15211): at libcore.io.Posix.recvfromBytes(Native Method)
07-22 23:18:43.698: W/System.err(15211): at libcore.io.Posix.recvfrom(Posix.java:131)
07-22 23:18:43.698: W/System.err(15211): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
07-22 23:18:43.698: W/System.err(15211): at libcore.io.IoBridge.recvfrom(IoBridge.java:503)
07-22 23:18:43.698: W/System.err(15211): ... 25 more
07-22 23:32:06.568: W/ActivityThread(15388): Application com.example.nslocatesystem is waiting for the debugger on port 8100...
07-22 23:32:09.348: W/ResponseProcessCookies(15388): Invalid cookie header: "Set-Cookie: BAIDUID=D7E2D6F740A54F783B4C622F50DA1C11:FG=1; max-age=31536000; expires=Wed, 17-Dec-14 06:04:26 GMT; domain=.baidu.com; path=/; version=1". Unable to parse expires attribute: Wed
如果真是这样,哪为什么用UC或别的浏览器都可以访问这个接口呢?
换成WIFI也行,就是打开移动网络就不可以了我还有一台手机是联通3G的,使用刷刷的一点问题都没有
如果真是这样,哪为什么用UC或别的浏览器都可以访问这个接口呢?
换成WIFI也行,就是打开移动网络就不可以了我还有一台手机是联通3G的,使用刷刷的一点问题都没有
别的浏览器或者别的方式能访问接口,不能证明服务端没有问题。之前我回答过一个问题,某个网址的gif能被其他浏览器打开,但是用android的webview就不行。结果证明是那个图片本身有问题,只是其他浏览器容错性好一点而已。
你可以试试你的程序访问别的网站,执行到getResponseCode()肯定是没有问题的。
建议你查查提交的url是否正确,并且检查服务端的日志来分析具体原因。
抛出的异常不怎么清晰,但大概的意思应该是端口被占用,但是为什么用移动的网络就端口被占用呢?WIFI、联通、电信的网都正常,这个会不会是移动公司的问题?
抛出的异常不怎么清晰,但大概的意思应该是端口被占用,但是为什么用移动的网络就端口被占用呢?WIFI、联通、电信的网都正常,这个会不会是移动公司的问题?