HttpURLConnection .getInputStream()在棉花糖上崩溃,而不是奥利奥

时间:2018-06-24 04:59:44

标签: android rest web

以下代码可在Android 8上完美运行。在运行6的平板电脑上,获取带有文件io错误的getinputstream时,它会崩溃。我已经尝试了HOURS和HOURS,但没有任何运气。请帮忙。什么都没有。即使尝试凌空也无济于事。我的Web API在计算机和电话上都能很好地工作。

@Override
protected String doInBackground(String... args) {
    StringBuilder result = new StringBuilder();

    try {
        URL url = new URL("https://api.github.com/users/dmnugent80/repos");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;

        while ((line = reader.readLine()) != null) {
            result.append(line);
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }


    return result.toString();
}

在此处记录猫


  

06-24 02:01:21.813 22840-22890 / com.marcusengineering.recipedatabase   I / System.out:[CDS] [DNS] getAllByNameImpl netId = 0 06-24 02:01:21.813   22840-22890 / com.marcusengineering.recipedatabase D / libc-netbsd:   [getaddrinfo]:hostname = recipedatabase.gear.host; servname =(null);   netid = 0;标记= 0       [getaddrinfo]:ai_addrlen = 0; ai_canonname =(null); ai_flags = 4; ai_family = 0 06-24 02:01:21.817   22840-22890 / com.marcusengineering.recipedatabase D / libc-netbsd:   [getaddrinfo]:hostname = recipedatabase.gear.host; servname =(null);   netid = 0; mark = 0 06-24 02:01:21.818   22840-22890 / com.marcusengineering.recipedatabase D / libc-netbsd:   [getaddrinfo]:ai_addrlen = 0; ai_canonname =(null); ai_flags = 1024;   ai_family = 0 06-24 02:01:21.824   22840-22890 / com.marcusengineering.recipedatabase D / libc-netbsd:   getaddrinfo:recipedatabase.gear.host从代理gai_error获取结果   = 0 06-24 24:01:21.824 22840-22890 / com.marcusengineering.recipedatabase I / System.out:[CDS] rx   超时:0 06-24 02:01:21.826   22840-22890 / com.marcusengineering.recipedatabase I / System.out:   [socket] [0]个连接   配方数据库.gear.host / 204.246.56.80:80; LocalPort = 43028(0)       [CDS] connect [recipedatabase.gear.host/204.246.56.80:80] 06-24 02:01:21.826 22840-22890 / com.marcusengineering.recipedatabase D / Posix:   [Posix_connect调试]处理com.marcusengineering.recipedatabase:80   06-24 02:01:21.919 22840-22890 / com.marcusengineering.recipedatabase   I / System.out:[CDS]端口[43028]       [socket] [/ 192.168.1.163:43028]已连接06-24 02:01:21.920 22840-22890 / com.marcusengineering.recipedatabase I / System.out:[CDS] rx   超时:0       [CDS] SO_SND_TIMEOUT:0 06-24 02:01:21.921 22840-22890 / com.marcusengineering.recipedatabase I / System.out:   [OkHttp] sendRequest >>       [OkHttp] sendRequest << 06-24 02:01:21.982 22840-22890 / com.marcusengineering.recipedatabase W / System.err:   java.io.FileNotFoundException:   http://recipedatabase.gear.host/RestServiceImpl.svc/jsonfindtable/SELECT   来自TblUnits的UnitID,UnitName,HiddenFlag排序依据JSON的UnitName   路径           在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)           在com.marcusengineering.recipedatabase.MainActivity.getRestFromWebAsString(MainActivity.java:334)           在com.marcusengineering.recipedatabase.MainActivity $ MyAsyncInitialLoad.doInBackground(MainActivity.java:252)           在com.marcusengineering.recipedatabase.MainActivity $ MyAsyncInitialLoad.doInBackground(MainActivity.java:232)           在android.os.AsyncTask $ 2.call(AsyncTask.java:295)06-24 02:01:21.983 22840-22890 / com.marcusengineering.recipedatabase   W / System.err:位于   java.util.concurrent.FutureTask.run(FutureTask.java:237)           在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)           在java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:0)

您应该首先检查响应代码以处理错误输入流。

<div id="myName">

</div>

<scrpit type="text/javascript">
   <?php
   if(isset($_SESSION['name']) && $_SESSION['name'] != ''){ ?>
       var myName =<?php echo $_SESSION['name'];?> ;
   <?php }
   else{
  ?>
      var myName = "NotSet";
   <?php } ?>;
  document.getElementById("myName").innerHTML=myName ;

document.write(myName);
</script>

然后打电话

InputStream inputs;
if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
            inputs = urlConnection.getInputStream();
        } else {
            // Error
            inputs = urlConnection.getErrorStream();
        }

由于处理错误流的麻烦,我还遇到了一些应用程序崩溃的经验。
请始终进行检查,这是一个好习惯。

答案 1 :(得分:0)

FINNNNNNNALY想通了!。

不确定100%需要哪一个,但这是我为帮助其他可怜的人所做的事情。

  1. 确保在未从WCF添加额外包装(即纯文本)的情况下对您的Web api进行格式化。 (不是很重要)
  2. 消除URL中的空格。看来OREO会为您执行此操作。早期版本不能。为此的代码是: URL url =新的URL(query.replace(“”,“%20”));
  3. 快乐的舞蹈

总的呼叫次数应如下:

            URL url = new URL(query.replace(" ","%20"));
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


        int i = urlConnection.getResponseCode();
        InputStream inputs;
        if (i < HttpURLConnection.HTTP_BAD_REQUEST) {
            inputs = urlConnection.getInputStream();
        } else {
            // Error
            inputs = urlConnection.getErrorStream();
            return "-1";
        }

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = "";

        while ((line = reader.readLine()) != null) {
            result.append(line);
        }

要在WCF中将格式设置为原始字符串,请参见以下内容。

在*****。cs

[OperationContract]
    [WebGet( UriTemplate = "jsonFindTableBare/{id}")] 
    Stream DoWork(string id);

在svc.cs

public Stream DoWork(String id)
    {

        WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
        String str = "YOUR RESPONSE HERE";
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
        return new MemoryStream(Encoding.UTF8.GetBytes(str));
    }

感谢所有帮助,Mangal。赞赏。