在本文中,我们将给您介绍关于调用Web服务时出现FileNotFoundException的详细内容,并且为您解答调用webservice出错的相关问题,此外,我们还将为您提供关于.net–IO.Fi
在本文中,我们将给您介绍关于调用Web服务时出现FileNotFoundException的详细内容,并且为您解答调用web service出错的相关问题,此外,我们还将为您提供关于.net – IO.FileNotFoundException但文件应该存在、Android - 在从 ContentResolver.insert() 返回的 Uri 上调用 openFileDescriptor 时出现 FileNotFoundException、android-尝试获取Instagram API的访问令牌时出现FileNotFoundException、A:重新安装应用程序后尝试访问MediaStore时出现FileNotFoundException的知识。
本文目录一览:- 调用Web服务时出现FileNotFoundException(调用web service出错)
- .net – IO.FileNotFoundException但文件应该存在
- Android - 在从 ContentResolver.insert() 返回的 Uri 上调用 openFileDescriptor 时出现 FileNotFoundException
- android-尝试获取Instagram API的访问令牌时出现FileNotFoundException
- A:重新安装应用程序后尝试访问MediaStore时出现FileNotFoundException
调用Web服务时出现FileNotFoundException(调用web service出错)
你好,我克服了最初的问题。我是一个完全的androidnoob,这是我的第一个应用程序。我正在Android模拟器上对此进行测试。我尝试在处连接到.NET
Web服务http://192.168.3.47/service.asmx
。我得到一个FileNotFoundException
。但是它在那里,URL是正确的。我怎样才能让他看到?
03-03 11:23:49.741: WARN/System.err(455): java.io.FileNotFoundException: http://192.168.3.47/service.asmx03-03 11:23:49.751: WARN/System.err(455): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)03-03 11:23:49.801: WARN/System.err(455): at gyozo.HelloWorld.HelloActivity.onClick(HelloActivity.java:62)03-03 11:23:49.831: WARN/System.err(455): at android.view.View.performClick(View.java:2485)03-03 11:23:49.851: WARN/System.err(455): at android.view.View$PerformClick.run(View.java:9080)03-03 11:23:49.871: WARN/System.err(455): at android.os.Handler.handleCallback(Handler.java:587)03-03 11:23:49.910: WARN/System.err(455): at android.os.Handler.dispatchMessage(Handler.java:92)03-03 11:23:49.940: WARN/System.err(455): at android.os.Looper.loop(Looper.java:123)03-03 11:23:49.950: WARN/System.err(455): at android.app.ActivityThread.main(ActivityThread.java:3683)03-03 11:23:50.010: WARN/System.err(455): at java.lang.reflect.Method.invokeNative(Native Method)03-03 11:23:50.050: WARN/System.err(455): at java.lang.reflect.Method.invoke(Method.java:507)03-03 11:23:50.070: WARN/System.err(455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)03-03 11:23:50.090: WARN/System.err(455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)03-03 11:23:50.110: WARN/System.err(455): at dalvik.system.NativeStart.main(Native Method)
这发生在这里: InputStream is = connection.getInputStream();
URL url = new URL("http://192.168.3.47/service.asmx");HttpURLConnection connection = (HttpURLConnection)url.openConnection();connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");connection.setUseCaches(false);connection.setDoInput(true);connection.setDoOutput(true);String soapRequest = String.format(getText(R.string.ws_listemain_ds_new).toString(), city, keyword);connection.setRequestProperty("Content-Length", Integer.toString(soapRequest.getBytes("UTF-8").length));//Send requestOutputStreamWriter owr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");owr.write(soapRequest);owr.flush();owr.close();//Get Response InputStream is = connection.getInputStream();
答案1
小编典典的HttpURLConnection
类是误导的,因为它会抛出FileNotFoundException
为400或以上的任何HTTP错误代码。
因此,它不一定是错误的URL(404),它可能是400(错误请求),403(禁止),500(内部服务器错误)或其他错误。
使用该getResponseCode
方法可以更准确地指示问题。
.net – IO.FileNotFoundException但文件应该存在
我只是想添加Logfiles(IO.FileInfo)作为电子邮件的附件,因此我试图检查每个文件的长度,以检测它们是否必须添加/压缩.
如果这些文件已存在,这可以正常工作.
但是,如果我在这次运行中创建它们,当我尝试检查长度时,我会遇到异常.奇怪的是,我可以写入这些“不存在的”文件(实际上FileInfo.Exists返回false),之前没有问题.
这是一些代码……
在名为Log的类的构造函数中创建其中一个文件:
Me.LogFile = New IO.FileInfo(infoLogPath) If Not LogFile.Exists() Then 'tried to use `Using` on the Stream but that doesn't change anything' Using stream = Me.LogFile.Create() 'close and dispose implicitely End Using End If
我可以毫无问题地写入文件:
Me.Log.WriteInfo("BlahBlahBlah...",False)
我在LogFile.Length上获得异常后的一行:
If Me.Log.LogFile.Length <> 0 Then files.Add(Me.Log.LogFile) End If
Me.Log是一个名为Log的自定义日志记录类对象,它保存对FileInfo对象的引用.
这是类Log中的WriteInfo,LogFile是IO.FileInfo-onject:
Public Sub WriteInfo(ByVal message As String,ByVal finishLog As Boolean) Try Using w As IO.StreamWriter = Me.LogFile.AppendText If Me.WithTimestamp Then w.WriteLine(Date.Now.ToString(Globalization.CultureInfo.InvariantCulture) & ": " & message) Else w.WriteLine(message) End If If finishLog Then w.WriteLine("__________________________") w.Flush() w.Close() End Using Catch writeLogException As Exception Try WriteError(writeLogException,True) Catch innerEx As Exception 'ignore End Try End Try End Sub
实际上@ShellShocks solution与Refresh很简单.从来没有听说过这个函数,奇怪的是,当我不刷新文件时,我得到了一个FileNotFoundException.
Me.Log.LogFile.Refresh()
Android - 在从 ContentResolver.insert() 返回的 Uri 上调用 openFileDescriptor 时出现 FileNotFoundException
如何解决Android - 在从 ContentResolver.insert() 返回的 Uri 上调用 openFileDescriptor 时出现 FileNotFoundException?
我参考了 Android 的 documentation 中的一个示例。我的目标是创建一个新文件,然后将其描述符传递给 MediaRecorder
中的 setoutputFile
。我有以下代码片段:
...
ContentResolver resolver = getApplicationContext()
.getContentResolver();
Uri videoCollection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
videoCollection = MediaStore.Video.Media
.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
videoCollection = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}
ContentValues videoDetails = new ContentValues();
videoDetails.put(MediaStore.Video.Media.disPLAY_NAME,"video.mp4");
Uri newVideoUri = resolver
.insert(videoCollection,videoDetails);
ParcelFileDescriptor file;
try {
file = resolver.openFileDescriptor(newVideoUri,"w");
} catch (FileNotFoundException e) {
e.printstacktrace();
}
...
令我惊讶的是,即使 resolver.insert() 返回一个 Uri,当尝试打开 fileDescriptor 时,会抛出一个带有消息的 FileNotFoundException
:
No such file or directory
。
我运行它的设备的 API 版本为 23。我做错了什么吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
android-尝试获取Instagram API的访问令牌时出现FileNotFoundException
尝试检索access_token时出现FileNotFoundException:
java.io.FileNotFoundException: https://api.instagram.com/oauth/access_token&client_id=e909da82f8544a70bb9b29434xxxxxx&client_secret=fa34037e0f534628bb9becd1a3xxxxxx&grant_type=authorization_code&redirect_uri=x-oauthflow-instagram://callback&code=520401255.e909da8.244c14ba79e842868a695192835c83ac
01-01 11:50:39.371: W/System.err(21868): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
错误发生在这一行
JSONObject jsonObj = (JSONObject) new JSONTokener(streamToString(urlConnection.getInputStream())).nextValue();
我究竟做错了什么?
解决方法:
只需将InstagramApp.java替换为您当前在库中的类.
package br.com.dina.oauth.instagram;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import br.com.dina.oauth.instagram.InstagramDialog.OAuthDialogListener;
/**
*
* @author Thiago Locatelli <thiago.locatelli@gmail.com>
* @author Lorensius W. L T <lorenz@londatiga.net>
*
*/
public class InstagramApp {
private InstagramSession mSession;
private InstagramDialog mDialog;
private OAuthAuthenticationListener mListener;
private ProgressDialog mProgress;
private String mAuthUrl;
private String mTokenUrl;
private String mAccesstoken;
private Context mCtx;
private String mClientId;
private String mClientSecret;
private static int WHAT_FINALIZE = 0;
private static int WHAT_ERROR = 1;
private static int WHAT_FETCH_INFO = 2;
/**
* Callback url, as set in 'Manage OAuth Costumers' page
* (https://developer.github.com/)
*/
public static String mCallbackUrl = "";
private static final String AUTH_URL = "https://api.instagram.com/oauth/authorize/";
private static final String TOKEN_URL = "https://api.instagram.com/oauth/access_token";
private static final String API_URL = "https://api.instagram.com/v1";
private static final String TAG = "InstagramAPI";
public InstagramApp(Context context, String clientId, String clientSecret,
String callbackUrl) {
mClientId = clientId;
mClientSecret = clientSecret;
mCtx = context;
mSession = new InstagramSession(context);
mAccesstoken = mSession.getAccesstoken();
mCallbackUrl = callbackUrl;
mTokenUrl = TOKEN_URL + "?client_id=" + clientId + "&client_secret="
+ clientSecret + "&redirect_uri=" + mCallbackUrl + "&grant_type=authorization_code";
mAuthUrl = AUTH_URL + "?client_id=" + clientId + "&redirect_uri="
+ mCallbackUrl + "&response_type=code&display=touch&scope=likes+comments+relationships";
OAuthDialogListener listener = new OAuthDialogListener() {
@Override
public void onComplete(String code) {
getAccesstoken(code);
}
@Override
public void one rror(String error) {
mListener.onFail("Authorization Failed");
}
};
mDialog = new InstagramDialog(context, mAuthUrl, listener);
mProgress = new ProgressDialog(context);
mProgress.setCancelable(false);
}
private void getAccesstoken(final String code) {
mProgress.setMessage("Getting access token ...");
mProgress.show();
new Thread() {
@Override
public void run() {
Log.i(TAG, "Getting access token");
int what = WHAT_FETCH_INFO;
try {
URL url = new URL(TOKEN_URL);
//URL url = new URL(mTokenUrl + "&code=" + code);
Log.i(TAG, "opening Token URL " + url.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
//urlConnection.connect();
OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getoutputStream());
writer.write("client_id="+mClientId+
"&client_secret="+mClientSecret+
"&grant_type=authorization_code" +
"&redirect_uri="+mCallbackUrl+
"&code=" + code);
writer.flush();
String response = streamToString(urlConnection.getInputStream());
Log.i(TAG, "response " + response);
JSONObject jsonObj = (JSONObject) new JSONTokener(response).nextValue();
mAccesstoken = jsonObj.getString("access_token");
Log.i(TAG, "Got access token: " + mAccesstoken);
String id = jsonObj.getJSONObject("user").getString("id");
String user = jsonObj.getJSONObject("user").getString("username");
String name = jsonObj.getJSONObject("user").getString("full_name");
mSession.storeAccesstoken(mAccesstoken, id, user, name);
} catch (Exception ex) {
what = WHAT_ERROR;
ex.printstacktrace();
}
mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0));
}
}.start();
}
private void fetchUserName() {
mProgress.setMessage("Finalizing ...");
new Thread() {
@Override
public void run() {
Log.i(TAG, "Fetching user info");
int what = WHAT_FINALIZE;
try {
URL url = new URL(API_URL + "/users/" + mSession.getId() + "/?access_token=" + mAccesstoken);
Log.d(TAG, "opening URL " + url.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
//urlConnection.setDoOutput(true);
urlConnection.connect();
String response = streamToString(urlConnection.getInputStream());
System.out.println(response);
JSONObject jsonObj = (JSONObject) new JSONTokener(response).nextValue();
String name = jsonObj.getJSONObject("data").getString("full_name");
String bio = jsonObj.getJSONObject("data").getString("bio");
Log.i(TAG, "Got name: " + name + ", bio [" + bio + "]");
} catch (Exception ex) {
what = WHAT_ERROR;
ex.printstacktrace();
}
mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
}
}.start();
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == WHAT_ERROR) {
mProgress.dismiss();
if(msg.arg1 == 1) {
mListener.onFail("Failed to get access token");
}
else if(msg.arg1 == 2) {
mListener.onFail("Failed to get user information");
}
}
else if(msg.what == WHAT_FETCH_INFO) {
fetchUserName();
}
else {
mProgress.dismiss();
mListener.onSuccess();
}
}
};
public boolean hasAccesstoken() {
return (mAccesstoken == null) ? false : true;
}
public void setListener(OAuthAuthenticationListener listener) {
mListener = listener;
}
public String getUserName() {
return mSession.getUsername();
}
public String getId() {
return mSession.getId();
}
public String getName() {
return mSession.getName();
}
public void authorize() {
//Intent webAuthIntent = new Intent(Intent.ACTION_VIEW);
//webAuthIntent.setData(Uri.parse(AUTH_URL));
//mCtx.startActivity(webAuthIntent);
mDialog.show();
}
private String streamToString(InputStream is) throws IOException {
String str = "";
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} finally {
is.close();
}
str = sb.toString();
}
return str;
}
public void resetAccesstoken() {
if (mAccesstoken != null) {
mSession.resetAccesstoken();
mAccesstoken = null;
}
}
public interface OAuthAuthenticationListener {
public abstract void onSuccess();
public abstract void onFail(String error);
}
}
A:重新安装应用程序后尝试访问MediaStore时出现FileNotFoundException
如何解决A:重新安装应用程序后尝试访问MediaStore时出现FileNotFoundException?
我是一名新的 Android 开发人员。我正在创建一个小应用程序来捕获屏幕并保存到共享存储。我的应用程序面向 SDK 30。当我进行一些捕获时,我卸载并重新安装它。在 Api>=29 设备上,我可以读取和显示以前的版本捕获。但是,当我使用 API
@JvmStatic
fun savePhoto(context: Context,folderName: String,bitmap: Bitmap): gallery? {
val dirPath: String =
context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.path.plus(File.separator)
.plus(folderName)
val rootFolder = File(dirPath)
if (!rootFolder.exists()) {
rootFolder.mkdirs()
}
try {
val sdf = SimpleDateFormat("MM-dd-yyyy - (hh.mm.a)",Locale.US)
val fileName =
context.getString(R.string.app_name).plus(" - [" + sdf.format(Date()) + "]")
val Now = Date()
val quality = 100
val fos: OutputStream
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentValues = ContentValues()
contentValues.put(MediaStore.Images.Media.TITLE,fileName)
contentValues.put(MediaStore.Images.Media.disPLAY_NAME,fileName)
contentValues.put(
MediaStore.Images.Media.DESCRIPTION,context.getString(R.string.app_name)
)
contentValues.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg")
contentValues.put(MediaStore.Images.Media.DATE_ADDED,Now.time)
contentValues.put(MediaStore.Images.Media.DATE_TAKEN,Now.time)
contentValues.put(
MediaStore.Images.Media.RELATIVE_PATH,(Environment.DIRECTORY_PICTURES).plus(File.separator)
.plus(folderName)
)
val imageUri = context.contentResolver.insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues
)
fos = context.contentResolver.openOutputStream(Objects.requireNonNull(imageUri!!))!!
bitmap.compress(Bitmap.CompressFormat.JPEG,quality,fos)
fos.close()
return imageUri.toString()
} else {
val mPath =
(rootFolder.absolutePath).plus(File.separator).plus(Now.time).plus(".jpg")
fos = FileOutputStream(mPath)
val imageFile = File(mPath)
if (!imageFile.exists()) {
imageFile.createNewFile()
}
bitmap.compress(Bitmap.CompressFormat.JPEG,fos)
fos.close()
val uri = addImagetogallery(context,fileName,imageFile)
return uri.toString()
}
} catch (ex: Exception) {
return null
}
}
private fun addImagetogallery(
context: Context,title: String,filepath: File
): Uri? {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE,title)
values.put(MediaStore.Images.Media.disPLAY_NAME,title)
values.put(MediaStore.Images.Media.DESCRIPTION,context.getString(R.string.app_name))
values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg")
values.put(MediaStore.Images.Media.DATE_ADDED,System.currentTimeMillis())
values.put(MediaStore.Images.Media.DATA,filepath.toString())
return context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values)
}
我是否使用错误的方式在 Api
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
今天关于调用Web服务时出现FileNotFoundException和调用web service出错的分享就到这里,希望大家有所收获,若想了解更多关于.net – IO.FileNotFoundException但文件应该存在、Android - 在从 ContentResolver.insert() 返回的 Uri 上调用 openFileDescriptor 时出现 FileNotFoundException、android-尝试获取Instagram API的访问令牌时出现FileNotFoundException、A:重新安装应用程序后尝试访问MediaStore时出现FileNotFoundException等相关知识,可以在本站进行查询。
本文标签: