C# 在Android中获取输入流上的FileNotFoundException
我一直在尝试从WCF到Android获取数据响应,但在连接到WCF时出错 调用HttpHandlerC# 在Android中获取输入流上的FileNotFoundException,c#,android,wcf,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: " + j
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,即使我在邮递员上尝试了该URL并正常工作。我的代码有什么问题,知道吗?对于所有
Log.e()
调用,将e
作为第三个参数传递。然后,检查与IOException
关联的完整Java堆栈跟踪,始终从URL获取空响应。
。不,你总是有例外。你根本没有得到回应。“你没有向我们展示在你身上可能发生的一切。”我得到了一些常识FileNotFoundException@greenapps啊,很抱歉,我将编辑它,这表明您的服务器正在为该URL返回HTTP 404响应。