在Android中的输入流上获取FileNotFoundException

时间:2017-05-07 17:03:44

标签: c# android wcf

我一直在尝试从WCF向Android提供数据响应,但在连接到WCF时遇到错误。

调用HttpHandler

HttpHandler sh = new HttpHandler();
    String baseurl = "http://10.0.2.2:8080/Service1.svc/ViewAllStudent";
    String jsonStr = sh.makeServiceCall(baseurl);
    Log.e(TAG, "Response from url: " + jsonStr);

HttpHandler.java

public String makeServiceCall(String reqUrl) {
    String response = null;
    try {
        URL url = new URL(reqUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        response = convertStreamToString(in);
    } catch (MalformedURLException e) {
        e.printStackTrace();
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    } catch (ProtocolException e) {
        e.printStackTrace();
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, "IOException: " + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    return response;
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}

logcat的

05-08 00:39:10.052 3763-3780/? W/System.err: java.io.FileNotFoundException: http://10.0.2.2:8080/Service1.svc/ViewAllStudent
05-08 00:39:10.052 3763-3780/? W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
05-08 00:39:10.052 3763-3780/? W/System.err:     at json.aprivate.sch.diesirepurejson.HttpHandler.makeServiceCall(HttpHandler.java:31)
05-08 00:39:10.052 3763-3780/? W/System.err:     at json.aprivate.sch.diesirepurejson.MainActivity$GetContacts.doInBackground(MainActivity.java:50)
05-08 00:39:10.052 3763-3780/? W/System.err:     at json.aprivate.sch.diesirepurejson.MainActivity$GetContacts.doInBackground(MainActivity.java:37)
05-08 00:39:10.052 3763-3780/? W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:305)
05-08 00:39:10.052 3763-3780/? W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-08 00:39:10.052 3763-3780/? W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
05-08 00:39:10.052 3763-3780/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
05-08 00:39:10.052 3763-3780/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
05-08 00:39:10.052 3763-3780/? W/System.err:     at java.lang.Thread.run(Thread.java:761)
05-08 00:39:10.052 3763-3780/? E/HttpHandler: IOException: http://10.0.2.2:8080/Service1.svc/ViewAllStudent
05-08 00:39:10.052 3763-3780/? E/MainActivity: Response from url: null
05-08 00:39:10.052 3763-3780/? E/MainActivity: Couldn't get json from server.

我总是从URL获取FileNotFoudException,即使我在postman上尝试URL并且正常工作。我的代码有什么不对,有什么想法吗?

0 个答案:

没有答案